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 | export interface Methods {
|
53 | [methodName: string]: {
|
54 | reqOpts?: r.CoreOptions;
|
55 | } | boolean;
|
56 | }
|
57 | export interface InstanceResponseCallback<T> {
|
58 | (err: ApiError | null, instance?: T | null, apiResponse?: r.Response): void;
|
59 | }
|
60 | export interface CreateOptions {
|
61 | }
|
62 | export declare type CreateResponse<T> = any[];
|
63 | export interface CreateCallback<T> {
|
64 | (err: ApiError | null, instance?: T | null, ...args: any[]): void;
|
65 | }
|
66 | export declare type DeleteOptions = {
|
67 | ignoreNotFound?: boolean;
|
68 | } & object;
|
69 | export interface DeleteCallback {
|
70 | (err: Error | null, apiResponse?: r.Response): void;
|
71 | }
|
72 | export interface GetConfig {
|
73 | |
74 |
|
75 |
|
76 | autoCreate?: boolean;
|
77 | }
|
78 | declare type GetOrCreateOptions = GetConfig & CreateOptions;
|
79 | export declare type GetResponse<T> = [T, r.Response];
|
80 | export interface ResponseCallback {
|
81 | (err?: Error | null, apiResponse?: r.Response): void;
|
82 | }
|
83 | export declare type SetMetadataResponse = [Metadata];
|
84 | export declare type SetMetadataOptions = object;
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | declare 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 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | delete(options?: DeleteOptions): Promise<[r.Response]>;
|
126 | delete(options: DeleteOptions, callback: DeleteCallback): void;
|
127 | delete(callback: DeleteCallback): void;
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | exists(options?: ExistsOptions): Promise<[boolean]>;
|
136 | exists(options: ExistsOptions, callback: ExistsCallback): void;
|
137 | exists(callback: ExistsCallback): void;
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | get(options?: GetOrCreateOptions): Promise<GetResponse<T>>;
|
151 | get(callback: InstanceResponseCallback<T>): void;
|
152 | get(options: GetOrCreateOptions, callback: InstanceResponseCallback<T>): void;
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | getMetadata(options?: GetMetadataOptions): Promise<MetadataResponse>;
|
162 | getMetadata(options: GetMetadataOptions, callback: MetadataCallback): void;
|
163 | getMetadata(callback: MetadataCallback): void;
|
164 | |
165 |
|
166 |
|
167 | getRequestInterceptors(): Function[];
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
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 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | private request_;
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | request(reqOpts: DecorateRequestOptions): Promise<RequestResponse>;
|
198 | request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | requestStream(reqOpts: DecorateRequestOptions): r.Request;
|
206 | }
|
207 | export { ServiceObject };
|