UNPKG

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