@mlhub/deployments-ts-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.openapi-generator/FILES +38 -0
- package/.openapi-generator/VERSION +1 -0
- package/.openapi-generator-ignore +23 -0
- package/README.md +45 -0
- package/dist/apis/DeploymentsApi.d.ts +64 -0
- package/dist/apis/DeploymentsApi.js +264 -0
- package/dist/apis/StrategiesApi.d.ts +26 -0
- package/dist/apis/StrategiesApi.js +121 -0
- package/dist/apis/index.d.ts +2 -0
- package/dist/apis/index.js +20 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +21 -0
- package/dist/models/BadRequestResponse.d.ts +51 -0
- package/dist/models/BadRequestResponse.js +48 -0
- package/dist/models/ClientStrategySet.d.ts +52 -0
- package/dist/models/ClientStrategySet.js +50 -0
- package/dist/models/DeployModelWithStrategyBody.d.ts +39 -0
- package/dist/models/DeployModelWithStrategyBody.js +44 -0
- package/dist/models/DesiredState.d.ts +24 -0
- package/dist/models/DesiredState.js +39 -0
- package/dist/models/GpuResource.d.ts +39 -0
- package/dist/models/GpuResource.js +45 -0
- package/dist/models/ListDeploymentStrategiesResponse.d.ts +52 -0
- package/dist/models/ListDeploymentStrategiesResponse.js +49 -0
- package/dist/models/ModelDeployment.d.ts +126 -0
- package/dist/models/ModelDeployment.js +74 -0
- package/dist/models/ModelDeploymentInterface.d.ts +21 -0
- package/dist/models/ModelDeploymentInterface.js +48 -0
- package/dist/models/ModelDeploymentInterfaceOneOf.d.ts +28 -0
- package/dist/models/ModelDeploymentInterfaceOneOf.js +41 -0
- package/dist/models/ModelDeploymentResponse.d.ts +52 -0
- package/dist/models/ModelDeploymentResponse.js +49 -0
- package/dist/models/ModelReference.d.ts +33 -0
- package/dist/models/ModelReference.js +42 -0
- package/dist/models/NotFoundResponse.d.ts +51 -0
- package/dist/models/NotFoundResponse.js +48 -0
- package/dist/models/Operator.d.ts +33 -0
- package/dist/models/Operator.js +48 -0
- package/dist/models/ParallelismStrategy.d.ts +25 -0
- package/dist/models/ParallelismStrategy.js +40 -0
- package/dist/models/Parameter.d.ts +27 -0
- package/dist/models/Parameter.js +40 -0
- package/dist/models/ParameterSet.d.ts +34 -0
- package/dist/models/ParameterSet.js +43 -0
- package/dist/models/Platform.d.ts +31 -0
- package/dist/models/Platform.js +46 -0
- package/dist/models/ReplicaGroup.d.ts +40 -0
- package/dist/models/ReplicaGroup.js +45 -0
- package/dist/models/ResourceRequirements.d.ts +46 -0
- package/dist/models/ResourceRequirements.js +48 -0
- package/dist/models/RestApi.d.ts +27 -0
- package/dist/models/RestApi.js +40 -0
- package/dist/models/Rule.d.ts +40 -0
- package/dist/models/Rule.js +45 -0
- package/dist/models/RuleSet.d.ts +34 -0
- package/dist/models/RuleSet.js +43 -0
- package/dist/models/ServerErrorResponse.d.ts +51 -0
- package/dist/models/ServerErrorResponse.js +48 -0
- package/dist/models/State.d.ts +27 -0
- package/dist/models/State.js +42 -0
- package/dist/models/Strategy.d.ts +46 -0
- package/dist/models/Strategy.js +48 -0
- package/dist/models/Visibility.d.ts +23 -0
- package/dist/models/Visibility.js +38 -0
- package/dist/models/index.d.ts +26 -0
- package/dist/models/index.js +44 -0
- package/dist/runtime.d.ts +140 -0
- package/dist/runtime.js +430 -0
- package/package.json +15 -0
- package/src/apis/DeploymentsApi.ts +192 -0
- package/src/apis/StrategiesApi.ts +63 -0
- package/src/apis/index.ts +4 -0
- package/src/index.ts +5 -0
- package/src/models/BadRequestResponse.ts +88 -0
- package/src/models/ClientStrategySet.ts +107 -0
- package/src/models/DeployModelWithStrategyBody.ts +72 -0
- package/src/models/DesiredState.ts +37 -0
- package/src/models/GpuResource.ts +72 -0
- package/src/models/ListDeploymentStrategiesResponse.ts +95 -0
- package/src/models/ModelDeployment.ts +215 -0
- package/src/models/ModelDeploymentInterface.ts +48 -0
- package/src/models/ModelDeploymentInterfaceOneOf.ts +63 -0
- package/src/models/ModelDeploymentResponse.ts +95 -0
- package/src/models/ModelReference.ts +64 -0
- package/src/models/NotFoundResponse.ts +88 -0
- package/src/models/Operator.ts +46 -0
- package/src/models/ParallelismStrategy.ts +38 -0
- package/src/models/Parameter.ts +56 -0
- package/src/models/ParameterSet.ts +71 -0
- package/src/models/Platform.ts +44 -0
- package/src/models/ReplicaGroup.ts +83 -0
- package/src/models/ResourceRequirements.ts +87 -0
- package/src/models/RestApi.ts +56 -0
- package/src/models/Rule.ts +79 -0
- package/src/models/RuleSet.ts +71 -0
- package/src/models/ServerErrorResponse.ts +88 -0
- package/src/models/State.ts +40 -0
- package/src/models/Strategy.ts +91 -0
- package/src/models/Visibility.ts +36 -0
- package/src/models/index.ts +28 -0
- package/src/runtime.ts +320 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* MLHub Deployments API
|
|
5
|
+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 0.1.0
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { exists, mapValues } from '../runtime';
|
|
16
|
+
import {
|
|
17
|
+
ParameterSet,
|
|
18
|
+
ParameterSetFromJSON,
|
|
19
|
+
ParameterSetFromJSONTyped,
|
|
20
|
+
ParameterSetToJSON,
|
|
21
|
+
RuleSet,
|
|
22
|
+
RuleSetFromJSON,
|
|
23
|
+
RuleSetFromJSONTyped,
|
|
24
|
+
RuleSetToJSON,
|
|
25
|
+
} from './';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @export
|
|
30
|
+
* @interface Strategy
|
|
31
|
+
*/
|
|
32
|
+
export interface Strategy {
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @type {string}
|
|
36
|
+
* @memberof Strategy
|
|
37
|
+
*/
|
|
38
|
+
description?: string | null;
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @type {string}
|
|
42
|
+
* @memberof Strategy
|
|
43
|
+
*/
|
|
44
|
+
name: string;
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @type {ParameterSet}
|
|
48
|
+
* @memberof Strategy
|
|
49
|
+
*/
|
|
50
|
+
parameter_set?: ParameterSet;
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @type {Array<RuleSet>}
|
|
54
|
+
* @memberof Strategy
|
|
55
|
+
*/
|
|
56
|
+
rule_sets: Array<RuleSet>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function StrategyFromJSON(json: any): Strategy {
|
|
60
|
+
return StrategyFromJSONTyped(json, false);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function StrategyFromJSONTyped(json: any, ignoreDiscriminator: boolean): Strategy {
|
|
64
|
+
if ((json === undefined) || (json === null)) {
|
|
65
|
+
return json;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
|
|
69
|
+
'description': !exists(json, 'description') ? undefined : json['description'],
|
|
70
|
+
'name': json['name'],
|
|
71
|
+
'parameter_set': !exists(json, 'parameter_set') ? undefined : ParameterSetFromJSON(json['parameter_set']),
|
|
72
|
+
'rule_sets': ((json['rule_sets'] as Array<any>).map(RuleSetFromJSON)),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function StrategyToJSON(value?: Strategy | null): any {
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
if (value === null) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
|
|
85
|
+
'description': value.description,
|
|
86
|
+
'name': value.name,
|
|
87
|
+
'parameter_set': ParameterSetToJSON(value.parameter_set),
|
|
88
|
+
'rule_sets': ((value.rule_sets as Array<any>).map(RuleSetToJSON)),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* MLHub Deployments API
|
|
5
|
+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 0.1.0
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @export
|
|
18
|
+
* @enum {string}
|
|
19
|
+
*/
|
|
20
|
+
export enum Visibility {
|
|
21
|
+
Public = 'Public',
|
|
22
|
+
Private = 'Private'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function VisibilityFromJSON(json: any): Visibility {
|
|
26
|
+
return VisibilityFromJSONTyped(json, false);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function VisibilityFromJSONTyped(json: any, ignoreDiscriminator: boolean): Visibility {
|
|
30
|
+
return json as Visibility;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function VisibilityToJSON(value?: Visibility | null): any {
|
|
34
|
+
return value as any;
|
|
35
|
+
}
|
|
36
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export * from './BadRequestResponse';
|
|
4
|
+
export * from './ClientStrategySet';
|
|
5
|
+
export * from './DeployModelWithStrategyBody';
|
|
6
|
+
export * from './DesiredState';
|
|
7
|
+
export * from './GpuResource';
|
|
8
|
+
export * from './ListDeploymentStrategiesResponse';
|
|
9
|
+
export * from './ModelDeployment';
|
|
10
|
+
export * from './ModelDeploymentInterface';
|
|
11
|
+
export * from './ModelDeploymentInterfaceOneOf';
|
|
12
|
+
export * from './ModelDeploymentResponse';
|
|
13
|
+
export * from './ModelReference';
|
|
14
|
+
export * from './NotFoundResponse';
|
|
15
|
+
export * from './Operator';
|
|
16
|
+
export * from './ParallelismStrategy';
|
|
17
|
+
export * from './Parameter';
|
|
18
|
+
export * from './ParameterSet';
|
|
19
|
+
export * from './Platform';
|
|
20
|
+
export * from './ReplicaGroup';
|
|
21
|
+
export * from './ResourceRequirements';
|
|
22
|
+
export * from './RestApi';
|
|
23
|
+
export * from './Rule';
|
|
24
|
+
export * from './RuleSet';
|
|
25
|
+
export * from './ServerErrorResponse';
|
|
26
|
+
export * from './State';
|
|
27
|
+
export * from './Strategy';
|
|
28
|
+
export * from './Visibility';
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* MLHub Deployments API
|
|
5
|
+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 0.1.0
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export const BASE_PATH = "http://localhost".replace(/\/+$/, "");
|
|
17
|
+
|
|
18
|
+
const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This is the base class for all generated API classes.
|
|
22
|
+
*/
|
|
23
|
+
export class BaseAPI {
|
|
24
|
+
|
|
25
|
+
private middleware: Middleware[];
|
|
26
|
+
|
|
27
|
+
constructor(protected configuration = new Configuration()) {
|
|
28
|
+
this.middleware = configuration.middleware;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
withMiddleware<T extends BaseAPI>(this: T, ...middlewares: Middleware[]) {
|
|
32
|
+
const next = this.clone<T>();
|
|
33
|
+
next.middleware = next.middleware.concat(...middlewares);
|
|
34
|
+
return next;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
withPreMiddleware<T extends BaseAPI>(this: T, ...preMiddlewares: Array<Middleware['pre']>) {
|
|
38
|
+
const middlewares = preMiddlewares.map((pre) => ({ pre }));
|
|
39
|
+
return this.withMiddleware<T>(...middlewares);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
withPostMiddleware<T extends BaseAPI>(this: T, ...postMiddlewares: Array<Middleware['post']>) {
|
|
43
|
+
const middlewares = postMiddlewares.map((post) => ({ post }));
|
|
44
|
+
return this.withMiddleware<T>(...middlewares);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async request(context: RequestOpts, initOverrides?: RequestInit): Promise<Response> {
|
|
48
|
+
const { url, init } = this.createFetchParams(context, initOverrides);
|
|
49
|
+
const response = await this.fetchApi(url, init);
|
|
50
|
+
if (response.status >= 200 && response.status < 300) {
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
throw response;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) {
|
|
57
|
+
let url = this.configuration.basePath + context.path;
|
|
58
|
+
if (context.query !== undefined && Object.keys(context.query).length !== 0) {
|
|
59
|
+
// only add the querystring to the URL if there are query parameters.
|
|
60
|
+
// this is done to avoid urls ending with a "?" character which buggy webservers
|
|
61
|
+
// do not handle correctly sometimes.
|
|
62
|
+
url += '?' + this.configuration.queryParamsStringify(context.query);
|
|
63
|
+
}
|
|
64
|
+
const body = ((typeof FormData !== "undefined" && context.body instanceof FormData) || context.body instanceof URLSearchParams || isBlob(context.body))
|
|
65
|
+
? context.body
|
|
66
|
+
: JSON.stringify(context.body);
|
|
67
|
+
|
|
68
|
+
const headers = Object.assign({}, this.configuration.headers, context.headers);
|
|
69
|
+
const init = {
|
|
70
|
+
method: context.method,
|
|
71
|
+
headers: headers,
|
|
72
|
+
body,
|
|
73
|
+
credentials: this.configuration.credentials,
|
|
74
|
+
...initOverrides
|
|
75
|
+
};
|
|
76
|
+
return { url, init };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private fetchApi = async (url: string, init: RequestInit) => {
|
|
80
|
+
let fetchParams = { url, init };
|
|
81
|
+
for (const middleware of this.middleware) {
|
|
82
|
+
if (middleware.pre) {
|
|
83
|
+
fetchParams = await middleware.pre({
|
|
84
|
+
fetch: this.fetchApi,
|
|
85
|
+
...fetchParams,
|
|
86
|
+
}) || fetchParams;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
let response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
|
|
90
|
+
for (const middleware of this.middleware) {
|
|
91
|
+
if (middleware.post) {
|
|
92
|
+
response = await middleware.post({
|
|
93
|
+
fetch: this.fetchApi,
|
|
94
|
+
url: fetchParams.url,
|
|
95
|
+
init: fetchParams.init,
|
|
96
|
+
response: response.clone(),
|
|
97
|
+
}) || response;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create a shallow clone of `this` by constructing a new instance
|
|
105
|
+
* and then shallow cloning data members.
|
|
106
|
+
*/
|
|
107
|
+
private clone<T extends BaseAPI>(this: T): T {
|
|
108
|
+
const constructor = this.constructor as any;
|
|
109
|
+
const next = new constructor(this.configuration);
|
|
110
|
+
next.middleware = this.middleware.slice();
|
|
111
|
+
return next;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export class RequiredError extends Error {
|
|
116
|
+
name: "RequiredError" = "RequiredError";
|
|
117
|
+
constructor(public field: string, msg?: string) {
|
|
118
|
+
super(msg);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const COLLECTION_FORMATS = {
|
|
123
|
+
csv: ",",
|
|
124
|
+
ssv: " ",
|
|
125
|
+
tsv: "\t",
|
|
126
|
+
pipes: "|",
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export type FetchAPI = WindowOrWorkerGlobalScope['fetch'];
|
|
130
|
+
|
|
131
|
+
export interface ConfigurationParameters {
|
|
132
|
+
basePath?: string; // override base path
|
|
133
|
+
fetchApi?: FetchAPI; // override for fetch implementation
|
|
134
|
+
middleware?: Middleware[]; // middleware to apply before/after fetch requests
|
|
135
|
+
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
|
|
136
|
+
username?: string; // parameter for basic security
|
|
137
|
+
password?: string; // parameter for basic security
|
|
138
|
+
apiKey?: string | ((name: string) => string); // parameter for apiKey security
|
|
139
|
+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
|
|
140
|
+
headers?: HTTPHeaders; //header params we want to use on every request
|
|
141
|
+
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class Configuration {
|
|
145
|
+
constructor(private configuration: ConfigurationParameters = {}) {}
|
|
146
|
+
|
|
147
|
+
get basePath(): string {
|
|
148
|
+
return this.configuration.basePath != null ? this.configuration.basePath : BASE_PATH;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
get fetchApi(): FetchAPI | undefined {
|
|
152
|
+
return this.configuration.fetchApi;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get middleware(): Middleware[] {
|
|
156
|
+
return this.configuration.middleware || [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
get queryParamsStringify(): (params: HTTPQuery) => string {
|
|
160
|
+
return this.configuration.queryParamsStringify || querystring;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get username(): string | undefined {
|
|
164
|
+
return this.configuration.username;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get password(): string | undefined {
|
|
168
|
+
return this.configuration.password;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
get apiKey(): ((name: string) => string) | undefined {
|
|
172
|
+
const apiKey = this.configuration.apiKey;
|
|
173
|
+
if (apiKey) {
|
|
174
|
+
return typeof apiKey === 'function' ? apiKey : () => apiKey;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get accessToken(): ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined {
|
|
180
|
+
const accessToken = this.configuration.accessToken;
|
|
181
|
+
if (accessToken) {
|
|
182
|
+
return typeof accessToken === 'function' ? accessToken : async () => accessToken;
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
get headers(): HTTPHeaders | undefined {
|
|
188
|
+
return this.configuration.headers;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
get credentials(): RequestCredentials | undefined {
|
|
192
|
+
return this.configuration.credentials;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export type Json = any;
|
|
197
|
+
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
198
|
+
export type HTTPHeaders = { [key: string]: string };
|
|
199
|
+
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
|
|
200
|
+
export type HTTPBody = Json | FormData | URLSearchParams;
|
|
201
|
+
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
|
|
202
|
+
|
|
203
|
+
export interface FetchParams {
|
|
204
|
+
url: string;
|
|
205
|
+
init: RequestInit;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface RequestOpts {
|
|
209
|
+
path: string;
|
|
210
|
+
method: HTTPMethod;
|
|
211
|
+
headers: HTTPHeaders;
|
|
212
|
+
query?: HTTPQuery;
|
|
213
|
+
body?: HTTPBody;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function exists(json: any, key: string) {
|
|
217
|
+
const value = json[key];
|
|
218
|
+
return value !== null && value !== undefined;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function querystring(params: HTTPQuery, prefix: string = ''): string {
|
|
222
|
+
return Object.keys(params)
|
|
223
|
+
.map((key) => {
|
|
224
|
+
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
|
|
225
|
+
const value = params[key];
|
|
226
|
+
if (value instanceof Array) {
|
|
227
|
+
const multiValue = value.map(singleValue => encodeURIComponent(String(singleValue)))
|
|
228
|
+
.join(`&${encodeURIComponent(fullKey)}=`);
|
|
229
|
+
return `${encodeURIComponent(fullKey)}=${multiValue}`;
|
|
230
|
+
}
|
|
231
|
+
if (value instanceof Date) {
|
|
232
|
+
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(value.toISOString())}`;
|
|
233
|
+
}
|
|
234
|
+
if (value instanceof Object) {
|
|
235
|
+
return querystring(value as HTTPQuery, fullKey);
|
|
236
|
+
}
|
|
237
|
+
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
|
|
238
|
+
})
|
|
239
|
+
.filter(part => part.length > 0)
|
|
240
|
+
.join('&');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function mapValues(data: any, fn: (item: any) => any) {
|
|
244
|
+
return Object.keys(data).reduce(
|
|
245
|
+
(acc, key) => ({ ...acc, [key]: fn(data[key]) }),
|
|
246
|
+
{}
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function canConsumeForm(consumes: Consume[]): boolean {
|
|
251
|
+
for (const consume of consumes) {
|
|
252
|
+
if ('multipart/form-data' === consume.contentType) {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface Consume {
|
|
260
|
+
contentType: string
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export interface RequestContext {
|
|
264
|
+
fetch: FetchAPI;
|
|
265
|
+
url: string;
|
|
266
|
+
init: RequestInit;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface ResponseContext {
|
|
270
|
+
fetch: FetchAPI;
|
|
271
|
+
url: string;
|
|
272
|
+
init: RequestInit;
|
|
273
|
+
response: Response;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface Middleware {
|
|
277
|
+
pre?(context: RequestContext): Promise<FetchParams | void>;
|
|
278
|
+
post?(context: ResponseContext): Promise<Response | void>;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface ApiResponse<T> {
|
|
282
|
+
raw: Response;
|
|
283
|
+
value(): Promise<T>;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export interface ResponseTransformer<T> {
|
|
287
|
+
(json: any): T;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export class JSONApiResponse<T> {
|
|
291
|
+
constructor(public raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {}
|
|
292
|
+
|
|
293
|
+
async value(): Promise<T> {
|
|
294
|
+
return this.transformer(await this.raw.json());
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export class VoidApiResponse {
|
|
299
|
+
constructor(public raw: Response) {}
|
|
300
|
+
|
|
301
|
+
async value(): Promise<void> {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export class BlobApiResponse {
|
|
307
|
+
constructor(public raw: Response) {}
|
|
308
|
+
|
|
309
|
+
async value(): Promise<Blob> {
|
|
310
|
+
return await this.raw.blob();
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export class TextApiResponse {
|
|
315
|
+
constructor(public raw: Response) {}
|
|
316
|
+
|
|
317
|
+
async value(): Promise<string> {
|
|
318
|
+
return await this.raw.text();
|
|
319
|
+
};
|
|
320
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true,
|
|
4
|
+
"target": "es5",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"lib": [
|
|
9
|
+
"es6",
|
|
10
|
+
"dom"
|
|
11
|
+
],
|
|
12
|
+
"typeRoots": [
|
|
13
|
+
"node_modules/@types"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"exclude": [
|
|
17
|
+
"dist",
|
|
18
|
+
"node_modules"
|
|
19
|
+
]
|
|
20
|
+
}
|