UNPKG

8.27 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}
52export interface Methods {
53 [methodName: string]: {
54 reqOpts?: r.CoreOptions;
55 } | boolean;
56}
57export interface InstanceResponseCallback<T> {
58 (err: ApiError | null, instance?: T | null, apiResponse?: r.Response): void;
59}
60export interface CreateOptions {
61}
62export declare type CreateResponse<T> = any[];
63export interface CreateCallback<T> {
64 (err: ApiError | null, instance?: T | null, ...args: any[]): void;
65}
66export declare type DeleteOptions = {
67 ignoreNotFound?: boolean;
68} & object;
69export interface DeleteCallback {
70 (err: Error | null, apiResponse?: r.Response): void;
71}
72export interface GetConfig {
73 /**
74 * Create the object if it doesn't already exist.
75 */
76 autoCreate?: boolean;
77}
78declare type GetOrCreateOptions = GetConfig & CreateOptions;
79export declare type GetResponse<T> = [T, r.Response];
80export interface ResponseCallback {
81 (err?: Error | null, apiResponse?: r.Response): void;
82}
83export declare type SetMetadataResponse = [Metadata];
84export declare type SetMetadataOptions = object;
85/**
86 * ServiceObject is a base class, meant to be inherited from by a "service
87 * object," like a BigQuery dataset or Storage bucket.
88 *
89 * Most of the time, these objects share common functionality; they can be
90 * created or deleted, and you can get or set their metadata.
91 *
92 * By inheriting from this class, a service object will be extended with these
93 * shared behaviors. Note that any method can be overridden when the service
94 * object requires specific behavior.
95 */
96declare class ServiceObject<T = any> extends EventEmitter {
97 metadata: Metadata;
98 baseUrl?: string;
99 parent: ServiceObjectParent;
100 id?: string;
101 pollIntervalMs?: number;
102 private createMethod?;
103 protected methods: Methods;
104 interceptors: Interceptor[];
105 constructor(config: ServiceObjectConfig);
106 /**
107 * Create the object.
108 *
109 * @param {object=} options - Configuration object.
110 * @param {function} callback - The callback function.
111 * @param {?error} callback.err - An error returned while making this request.
112 * @param {object} callback.instance - The instance.
113 * @param {object} callback.apiResponse - The full API response.
114 */
115 create(options?: CreateOptions): Promise<CreateResponse<T>>;
116 create(options: CreateOptions, callback: CreateCallback<T>): void;
117 create(callback: CreateCallback<T>): void;
118 /**
119 * Delete the object.
120 *
121 * @param {function=} callback - The callback function.
122 * @param {?error} callback.err - An error returned while making this request.
123 * @param {object} callback.apiResponse - The full API response.
124 */
125 delete(options?: DeleteOptions): Promise<[r.Response]>;
126 delete(options: DeleteOptions, callback: DeleteCallback): void;
127 delete(callback: DeleteCallback): void;
128 /**
129 * Check if the object exists.
130 *
131 * @param {function} callback - The callback function.
132 * @param {?error} callback.err - An error returned while making this request.
133 * @param {boolean} callback.exists - Whether the object exists or not.
134 */
135 exists(options?: ExistsOptions): Promise<[boolean]>;
136 exists(options: ExistsOptions, callback: ExistsCallback): void;
137 exists(callback: ExistsCallback): void;
138 /**
139 * Get the object if it exists. Optionally have the object created if an
140 * options object is provided with `autoCreate: true`.
141 *
142 * @param {object=} options - The configuration object that will be used to
143 * create the object if necessary.
144 * @param {boolean} options.autoCreate - Create the object if it doesn't already exist.
145 * @param {function} callback - The callback function.
146 * @param {?error} callback.err - An error returned while making this request.
147 * @param {object} callback.instance - The instance.
148 * @param {object} callback.apiResponse - The full API response.
149 */
150 get(options?: GetOrCreateOptions): Promise<GetResponse<T>>;
151 get(callback: InstanceResponseCallback<T>): void;
152 get(options: GetOrCreateOptions, callback: InstanceResponseCallback<T>): void;
153 /**
154 * Get the metadata of this object.
155 *
156 * @param {function} callback - The callback function.
157 * @param {?error} callback.err - An error returned while making this request.
158 * @param {object} callback.metadata - The metadata for this object.
159 * @param {object} callback.apiResponse - The full API response.
160 */
161 getMetadata(options?: GetMetadataOptions): Promise<MetadataResponse>;
162 getMetadata(options: GetMetadataOptions, callback: MetadataCallback): void;
163 getMetadata(callback: MetadataCallback): void;
164 /**
165 * Return the user's custom request interceptors.
166 */
167 getRequestInterceptors(): Function[];
168 /**
169 * Set the metadata for this object.
170 *
171 * @param {object} metadata - The metadata to set on this object.
172 * @param {object=} options - Configuration options.
173 * @param {function=} callback - The callback function.
174 * @param {?error} callback.err - An error returned while making this request.
175 * @param {object} callback.apiResponse - The full API response.
176 */
177 setMetadata(metadata: Metadata, options?: SetMetadataOptions): Promise<SetMetadataResponse>;
178 setMetadata(metadata: Metadata, callback: MetadataCallback): void;
179 setMetadata(metadata: Metadata, options: SetMetadataOptions, callback: MetadataCallback): void;
180 /**
181 * Make an authenticated API request.
182 *
183 * @private
184 *
185 * @param {object} reqOpts - Request options that are passed to `request`.
186 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
187 * @param {function} callback - The callback function passed to `request`.
188 */
189 private request_;
190 /**
191 * Make an authenticated API request.
192 *
193 * @param {object} reqOpts - Request options that are passed to `request`.
194 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
195 * @param {function} callback - The callback function passed to `request`.
196 */
197 request(reqOpts: DecorateRequestOptions): Promise<RequestResponse>;
198 request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
199 /**
200 * Make an authenticated API request.
201 *
202 * @param {object} reqOpts - Request options that are passed to `request`.
203 * @param {string} reqOpts.uri - A URI relative to the baseUrl.
204 */
205 requestStream(reqOpts: DecorateRequestOptions): r.Request;
206}
207export { ServiceObject };