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