1 | import { EventEmitter } from 'events';
|
2 | import * as r from 'teeny-request';
|
3 | import { ApiError, BodyResponseCallback, DecorateRequestOptions } from './util.js';
|
4 | export type RequestResponse = [unknown, r.Response];
|
5 | export interface ServiceObjectParent {
|
6 | interceptors: Interceptor[];
|
7 | getRequestInterceptors(): Function[];
|
8 | requestStream(reqOpts: DecorateRequestOptions): r.Request;
|
9 | request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
|
10 | }
|
11 | export interface Interceptor {
|
12 | request(opts: r.Options): DecorateRequestOptions;
|
13 | }
|
14 | export type GetMetadataOptions = object;
|
15 | export type MetadataResponse<K> = [K, r.Response];
|
16 | export type MetadataCallback<K> = (err: Error | null, metadata?: K, apiResponse?: r.Response) => void;
|
17 | export type ExistsOptions = object;
|
18 | export interface ExistsCallback {
|
19 | (err: Error | null, exists?: boolean): void;
|
20 | }
|
21 | export interface ServiceObjectConfig {
|
22 | |
23 |
|
24 |
|
25 | baseUrl?: string;
|
26 | |
27 |
|
28 |
|
29 | createMethod?: Function;
|
30 | |
31 |
|
32 |
|
33 |
|
34 | id?: string;
|
35 | |
36 |
|
37 |
|
38 | methods?: Methods;
|
39 | |
40 |
|
41 |
|
42 |
|
43 | parent: ServiceObjectParent;
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | projectId?: string;
|
50 | }
|
51 | export interface Methods {
|
52 | [methodName: string]: {
|
53 | reqOpts?: r.CoreOptions;
|
54 | } | boolean;
|
55 | }
|
56 | export interface InstanceResponseCallback<T> {
|
57 | (err: ApiError | null, instance?: T | null, apiResponse?: r.Response): void;
|
58 | }
|
59 | export interface CreateOptions {
|
60 | }
|
61 | export type CreateResponse<T> = any[];
|
62 | export interface CreateCallback<T> {
|
63 | (err: ApiError | null, instance?: T | null, ...args: any[]): void;
|
64 | }
|
65 | export type DeleteOptions = {
|
66 | ignoreNotFound?: boolean;
|
67 | ifGenerationMatch?: number | string;
|
68 | ifGenerationNotMatch?: number | string;
|
69 | ifMetagenerationMatch?: number | string;
|
70 | ifMetagenerationNotMatch?: number | string;
|
71 | } & object;
|
72 | export interface DeleteCallback {
|
73 | (err: Error | null, apiResponse?: r.Response): void;
|
74 | }
|
75 | export interface GetConfig {
|
76 | |
77 |
|
78 |
|
79 | autoCreate?: boolean;
|
80 | }
|
81 | export type GetOrCreateOptions = GetConfig & CreateOptions;
|
82 | export type GetResponse<T> = [T, r.Response];
|
83 | export interface ResponseCallback {
|
84 | (err?: Error | null, apiResponse?: r.Response): void;
|
85 | }
|
86 | export type SetMetadataResponse<K> = [K];
|
87 | export type SetMetadataOptions = object;
|
88 | export interface BaseMetadata {
|
89 | id?: string;
|
90 | kind?: string;
|
91 | etag?: string;
|
92 | selfLink?: string;
|
93 | [key: string]: unknown;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | declare 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 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | delete(options?: DeleteOptions): Promise<[r.Response]>;
|
136 | delete(options: DeleteOptions, callback: DeleteCallback): void;
|
137 | delete(callback: DeleteCallback): void;
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | exists(options?: ExistsOptions): Promise<[boolean]>;
|
146 | exists(options: ExistsOptions, callback: ExistsCallback): void;
|
147 | exists(callback: ExistsCallback): void;
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | get(options?: GetOrCreateOptions): Promise<GetResponse<T>>;
|
161 | get(callback: InstanceResponseCallback<T>): void;
|
162 | get(options: GetOrCreateOptions, callback: InstanceResponseCallback<T>): void;
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | getMetadata(options?: GetMetadataOptions): Promise<MetadataResponse<K>>;
|
172 | getMetadata(options: GetMetadataOptions, callback: MetadataCallback<K>): void;
|
173 | getMetadata(callback: MetadataCallback<K>): void;
|
174 | |
175 |
|
176 |
|
177 | getRequestInterceptors(): Function[];
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
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 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | private request_;
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | request(reqOpts: DecorateRequestOptions): Promise<RequestResponse>;
|
208 | request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | requestStream(reqOpts: DecorateRequestOptions): r.Request;
|
216 | }
|
217 | export { ServiceObject };
|