UNPKG

8.53 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from 'events';
3import * as r from 'teeny-request';
4import { ApiError, BodyResponseCallback, DecorateRequestOptions } from './util';
5export declare type RequestResponse = [Metadata, r.Response];
6export interface ServiceObjectParent {
7 interceptors: Interceptor[];
8 getRequestInterceptors(): Function[];
9 requestStream(reqOpts: DecorateRequestOptions): r.Request;
10 request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
11}
12export interface Interceptor {
13 request(opts: r.Options): DecorateRequestOptions;
14}
15export declare type GetMetadataOptions = object;
16export declare type Metadata = any;
17export declare type MetadataResponse = [Metadata, r.Response];
18export declare type MetadataCallback = (err: Error | null, metadata?: Metadata, apiResponse?: r.Response) => void;
19export declare type ExistsOptions = object;
20export interface ExistsCallback {
21 (err: Error | null, exists?: boolean): void;
22}
23export interface ServiceObjectConfig {
24 /**
25 * The base URL to make API requests to.
26 */
27 baseUrl?: string;
28 /**
29 * The method which creates this object.
30 */
31 createMethod?: Function;
32 /**
33 * The identifier of the object. For example, the name of a Storage bucket or
34 * Pub/Sub topic.
35 */
36 id?: string;
37 /**
38 * A map of each method name that should be inherited.
39 */
40 methods?: Methods;
41 /**
42 * The parent service instance. For example, an instance of Storage if the
43 * object is Bucket.
44 */
45 parent: ServiceObjectParent;
46 /**
47 * For long running operations, how often should the client poll
48 * for completion.
49 */
50 pollIntervalMs?: number;
51 /**
52 * Override of projectId, used to allow access to resources in another project.
53 * For example, a BigQuery dataset in another project to which the user has been
54 * granted permission.
55 */
56 projectId?: string;
57}
58export interface Methods {
59 [methodName: string]: {
60 reqOpts?: r.CoreOptions;
61 } | boolean;
62}
63export interface InstanceResponseCallback<T> {
64 (err: ApiError | null, instance?: T | null, apiResponse?: r.Response): void;
65}
66export interface CreateOptions {
67}
68export declare type CreateResponse<T> = any[];
69export interface CreateCallback<T> {
70 (err: ApiError | null, instance?: T | null, ...args: any[]): void;
71}
72export declare type DeleteOptions = {
73 ignoreNotFound?: boolean;
74} & object;
75export interface DeleteCallback {
76 (err: Error | null, apiResponse?: r.Response): void;
77}
78export interface GetConfig {
79 /**
80 * Create the object if it doesn't already exist.
81 */
82 autoCreate?: boolean;
83}
84declare type GetOrCreateOptions = GetConfig & CreateOptions;
85export declare type GetResponse<T> = [T, r.Response];
86export interface ResponseCallback {
87 (err?: Error | null, apiResponse?: r.Response): void;
88}
89export declare type SetMetadataResponse = [Metadata];
90export declare type SetMetadataOptions = object;
91/**
92 * ServiceObject is a base class, meant to be inherited from by a "service
93 * object," like a BigQuery dataset or Storage bucket.
94 *
95 * Most of the time, these objects share common functionality; they can be
96 * created or deleted, and you can get or set their metadata.
97 *
98 * By inheriting from this class, a service object will be extended with these
99 * shared behaviors. Note that any method can be overridden when the service
100 * object requires specific behavior.
101 */
102declare class ServiceObject<T = any> extends EventEmitter {
103 metadata: Metadata;
104 baseUrl?: string;
105 parent: ServiceObjectParent;
106 id?: string;
107 pollIntervalMs?: number;
108 private createMethod?;
109 protected methods: Methods;
110 interceptors: Interceptor[];
111 projectId?: string;
112 constructor(config: ServiceObjectConfig);
113 /**
114 * Create the object.
115 *
116 * @param {object=} options - Configuration object.
117 * @param {function} callback - The callback function.
118 * @param {?error} callback.err - An error returned while making this request.
119 * @param {object} callback.instance - The instance.
120 * @param {object} callback.apiResponse - The full API response.
121 */
122 create(options?: CreateOptions): Promise<CreateResponse<T>>;
123 create(options: CreateOptions, callback: CreateCallback<T>): void;
124 create(callback: CreateCallback<T>): void;
125 /**
126 * Delete the object.
127 *
128 * @param {function=} callback - The callback function.
129 * @param {?error} callback.err - An error returned while making this request.
130 * @param {object} callback.apiResponse - The full API response.
131 */
132 delete(options?: DeleteOptions): Promise<[r.Response]>;
133 delete(options: DeleteOptions, callback: DeleteCallback): void;
134 delete(callback: DeleteCallback): void;
135 /**
136 * Check if the object exists.
137 *
138 * @param {function} callback - The callback function.
139 * @param {?error} callback.err - An error returned while making this request.
140 * @param {boolean} callback.exists - Whether the object exists or not.
141 */
142 exists(options?: ExistsOptions): Promise<[boolean]>;
143 exists(options: ExistsOptions, callback: ExistsCallback): void;
144 exists(callback: ExistsCallback): void;
145 /**
146 * Get the object if it exists. Optionally have the object created if an
147 * options object is provided with `autoCreate: true`.
148 *
149 * @param {object=} options - The configuration object that will be used to
150 * create the object if necessary.
151 * @param {boolean} options.autoCreate - Create the object if it doesn't already exist.
152 * @param {function} callback - The callback function.
153 * @param {?error} callback.err - An error returned while making this request.
154 * @param {object} callback.instance - The instance.
155 * @param {object} callback.apiResponse - The full API response.
156 */
157 get(options?: GetOrCreateOptions): Promise<GetResponse<T>>;
158 get(callback: InstanceResponseCallback<T>): void;
159 get(options: GetOrCreateOptions, callback: InstanceResponseCallback<T>): void;
160 /**
161 * Get the metadata of this object.
162 *
163 * @param {function} callback - The callback function.
164 * @param {?error} callback.err - An error returned while making this request.
165 * @param {object} callback.metadata - The metadata for this object.
166 * @param {object} callback.apiResponse - The full API response.
167 */
168 getMetadata(options?: GetMetadataOptions): Promise<MetadataResponse>;
169 getMetadata(options: GetMetadataOptions, callback: MetadataCallback): void;
170 getMetadata(callback: MetadataCallback): void;
171 /**
172 * Return the user's custom request interceptors.
173 */
174 getRequestInterceptors(): Function[];
175 /**
176 * Set the metadata for this object.
177 *
178 * @param {object} metadata - The metadata to set on this object.
179 * @param {object=} options - Configuration options.
180 * @param {function=} callback - The callback function.
181 * @param {?error} callback.err - An error returned while making this request.
182 * @param {object} callback.apiResponse - The full API response.
183 */
184 setMetadata(metadata: Metadata, options?: SetMetadataOptions): Promise<SetMetadataResponse>;
185 setMetadata(metadata: Metadata, callback: MetadataCallback): void;
186 setMetadata(metadata: Metadata, options: SetMetadataOptions, callback: MetadataCallback): void;
187 /**
188 * Make an authenticated API request.
189 *
190 * @private
191 *
192 * @param {object} reqOpts - Request options that are passed to `request`.
193 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
194 * @param {function} callback - The callback function passed to `request`.
195 */
196 private request_;
197 /**
198 * Make an authenticated API request.
199 *
200 * @param {object} reqOpts - Request options that are passed to `request`.
201 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
202 * @param {function} callback - The callback function passed to `request`.
203 */
204 request(reqOpts: DecorateRequestOptions): Promise<RequestResponse>;
205 request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
206 /**
207 * Make an authenticated API request.
208 *
209 * @param {object} reqOpts - Request options that are passed to `request`.
210 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
211 */
212 requestStream(reqOpts: DecorateRequestOptions): r.Request;
213}
214export { ServiceObject };