@mittwald/api-client-commons 4.2.0-alpha.6 → 4.2.1
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/dist/{index.d.cts → index.d.mts} +3 -7
- package/dist/index.d.ts +3 -7
- package/dist/index.mjs +22 -0
- package/dist/{react.d.ts → react/index.d.mts} +3 -3
- package/dist/{react.d.cts → react/index.d.ts} +3 -3
- package/dist/react/index.mjs +49 -0
- package/dist/{chunk-PEYA5TMK.js → shared/api-client-commons.2a6e3962.mjs} +30 -56
- package/dist/{RequestFunction-b1412636.d.ts → shared/api-client-commons.8b686095.d.mts} +7 -3
- package/dist/shared/api-client-commons.8b686095.d.ts +75 -0
- package/package.json +51 -43
- package/dist/index.cjs +0 -196
- package/dist/index.js +0 -48
- package/dist/react.cjs +0 -185
- package/dist/react.js +0 -45
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, CreateAxiosDefaults, AxiosError, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig } from 'axios';
|
|
2
2
|
export * from 'axios';
|
|
3
|
-
import { R as Response, O as OpenAPIOperation, a as RequestFunction, A as AnyResponse, P as PathParameters, b as RequestObject, c as ResponsePromise } from './
|
|
4
|
-
export { e as AnyRequest,
|
|
3
|
+
import { R as Response, O as OpenAPIOperation, a as RequestFunction, A as AnyResponse, P as PathParameters, b as RequestObject, c as ResponsePromise } from './shared/api-client-commons.8b686095.mjs';
|
|
4
|
+
export { e as AnyRequest, n as HttpHeaders, l as HttpMediaType, m as HttpMethod, H as HttpPayload, k as HttpStatus, h as InferredRequestData, I as InferredRequestType, g as InferredResponseData, f as InferredResponseType, Q as QueryParameters, j as RequestData, d as RequestType, i as ResponseData } from './shared/api-client-commons.8b686095.mjs';
|
|
5
5
|
import 'type-fest';
|
|
6
6
|
|
|
7
7
|
type Simplify<T> = {
|
|
@@ -12,10 +12,6 @@ declare function assertStatus<T extends Response, S extends T["status"]>(respons
|
|
|
12
12
|
status: S;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
declare function assertOneOfStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S[]): asserts response is T & {
|
|
16
|
-
status: S;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
15
|
declare abstract class ApiClientBase {
|
|
20
16
|
axios: AxiosInstance;
|
|
21
17
|
constructor(axiosConfig?: AxiosInstance | CreateAxiosDefaults);
|
|
@@ -46,4 +42,4 @@ declare class Request<TOp extends OpenAPIOperation> {
|
|
|
46
42
|
private convertQueryToUrlSearchParams;
|
|
47
43
|
}
|
|
48
44
|
|
|
49
|
-
export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, Simplify,
|
|
45
|
+
export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, type Simplify, assertStatus };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, CreateAxiosDefaults, AxiosError, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig } from 'axios';
|
|
2
2
|
export * from 'axios';
|
|
3
|
-
import { R as Response, O as OpenAPIOperation, a as RequestFunction, A as AnyResponse, P as PathParameters, b as RequestObject, c as ResponsePromise } from './
|
|
4
|
-
export { e as AnyRequest,
|
|
3
|
+
import { R as Response, O as OpenAPIOperation, a as RequestFunction, A as AnyResponse, P as PathParameters, b as RequestObject, c as ResponsePromise } from './shared/api-client-commons.8b686095.js';
|
|
4
|
+
export { e as AnyRequest, n as HttpHeaders, l as HttpMediaType, m as HttpMethod, H as HttpPayload, k as HttpStatus, h as InferredRequestData, I as InferredRequestType, g as InferredResponseData, f as InferredResponseType, Q as QueryParameters, j as RequestData, d as RequestType, i as ResponseData } from './shared/api-client-commons.8b686095.js';
|
|
5
5
|
import 'type-fest';
|
|
6
6
|
|
|
7
7
|
type Simplify<T> = {
|
|
@@ -12,10 +12,6 @@ declare function assertStatus<T extends Response, S extends T["status"]>(respons
|
|
|
12
12
|
status: S;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
declare function assertOneOfStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S[]): asserts response is T & {
|
|
16
|
-
status: S;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
15
|
declare abstract class ApiClientBase {
|
|
20
16
|
axios: AxiosInstance;
|
|
21
17
|
constructor(axiosConfig?: AxiosInstance | CreateAxiosDefaults);
|
|
@@ -46,4 +42,4 @@ declare class Request<TOp extends OpenAPIOperation> {
|
|
|
46
42
|
private convertQueryToUrlSearchParams;
|
|
47
43
|
}
|
|
48
44
|
|
|
49
|
-
export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, Simplify,
|
|
45
|
+
export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, type Simplify, assertStatus };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import axios__default, { Axios } from 'axios';
|
|
2
|
+
export * from 'axios';
|
|
3
|
+
import { R as Request } from './shared/api-client-commons.2a6e3962.mjs';
|
|
4
|
+
export { A as ApiClientError, O as OpenAPIPath, a as assertStatus } from './shared/api-client-commons.2a6e3962.mjs';
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __publicField = (obj, key, value) => {
|
|
9
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
class ApiClientBase {
|
|
13
|
+
constructor(axiosConfig = axios__default) {
|
|
14
|
+
__publicField(this, "axios");
|
|
15
|
+
this.axios = axiosConfig instanceof Axios ? axiosConfig : axios__default.create(axiosConfig);
|
|
16
|
+
}
|
|
17
|
+
requestFunctionFactory(operation) {
|
|
18
|
+
return (conf) => new Request(operation, conf).execute(this.axios);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { ApiClientBase, Request };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { O as OpenAPIOperation, b as RequestObject, g as
|
|
1
|
+
import { O as OpenAPIOperation, b as RequestObject, g as InferredResponseData, a as RequestFunction } from '../shared/api-client-commons.8b686095.mjs';
|
|
2
2
|
import { AsyncResource } from '@mittwald/react-use-promise';
|
|
3
3
|
import 'axios';
|
|
4
4
|
import 'type-fest';
|
|
5
5
|
|
|
6
|
-
type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<
|
|
6
|
+
type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>>;
|
|
7
7
|
|
|
8
8
|
declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
|
|
9
9
|
private static namespace;
|
|
@@ -16,4 +16,4 @@ declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
|
|
|
16
16
|
getApiResource: GetApiResourceFn<TOp>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export { ApiCallAsyncResourceFactory
|
|
19
|
+
export { ApiCallAsyncResourceFactory };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { O as OpenAPIOperation, b as RequestObject, g as
|
|
1
|
+
import { O as OpenAPIOperation, b as RequestObject, g as InferredResponseData, a as RequestFunction } from '../shared/api-client-commons.8b686095.js';
|
|
2
2
|
import { AsyncResource } from '@mittwald/react-use-promise';
|
|
3
3
|
import 'axios';
|
|
4
4
|
import 'type-fest';
|
|
5
5
|
|
|
6
|
-
type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<
|
|
6
|
+
type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>>;
|
|
7
7
|
|
|
8
8
|
declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
|
|
9
9
|
private static namespace;
|
|
@@ -16,4 +16,4 @@ declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
|
|
|
16
16
|
getApiResource: GetApiResourceFn<TOp>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export { ApiCallAsyncResourceFactory
|
|
19
|
+
export { ApiCallAsyncResourceFactory };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getAsyncResource } from '@mittwald/react-use-promise';
|
|
2
|
+
import { R as Request, a as assertStatus } from '../shared/api-client-commons.2a6e3962.mjs';
|
|
3
|
+
import 'axios';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => {
|
|
8
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
const _ApiCallAsyncResourceFactory = class _ApiCallAsyncResourceFactory {
|
|
12
|
+
constructor(operation, requestFn) {
|
|
13
|
+
__publicField(this, "operation");
|
|
14
|
+
__publicField(this, "requestFn");
|
|
15
|
+
__publicField(this, "getApiResource", (requestObj) => {
|
|
16
|
+
const request = new Request(this.operation, requestObj);
|
|
17
|
+
return getAsyncResource(
|
|
18
|
+
(requestObj2) => this.executeRequest(requestObj2),
|
|
19
|
+
[requestObj],
|
|
20
|
+
{
|
|
21
|
+
tags: this.getAsyncResourceTags(request),
|
|
22
|
+
loaderId: this.getAsyncResourceId()
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
this.operation = operation;
|
|
27
|
+
this.requestFn = requestFn;
|
|
28
|
+
}
|
|
29
|
+
getAsyncResourceId() {
|
|
30
|
+
return `${_ApiCallAsyncResourceFactory.namespace}/${this.operation.operationId}`;
|
|
31
|
+
}
|
|
32
|
+
getAsyncResourceTags(request) {
|
|
33
|
+
const url = request.requestConfig.url ?? "";
|
|
34
|
+
return [
|
|
35
|
+
this.getAsyncResourceId(),
|
|
36
|
+
`${_ApiCallAsyncResourceFactory.namespace}/${this.operation.method}`,
|
|
37
|
+
`${_ApiCallAsyncResourceFactory.namespace}/${url}`
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
async executeRequest(requestObj) {
|
|
41
|
+
const response = await this.requestFn(requestObj);
|
|
42
|
+
assertStatus(response, 200);
|
|
43
|
+
return response.data;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
__publicField(_ApiCallAsyncResourceFactory, "namespace", "@mittwald/api-client");
|
|
47
|
+
let ApiCallAsyncResourceFactory = _ApiCallAsyncResourceFactory;
|
|
48
|
+
|
|
49
|
+
export { ApiCallAsyncResourceFactory };
|
|
@@ -1,29 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
1
|
+
import { AxiosError } from 'axios';
|
|
18
2
|
|
|
19
|
-
|
|
20
|
-
var
|
|
3
|
+
var __defProp$1 = Object.defineProperty;
|
|
4
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField$1 = (obj, key, value) => {
|
|
6
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
return value;
|
|
8
|
+
};
|
|
9
|
+
class OpenAPIPath {
|
|
21
10
|
constructor(rawPath, params) {
|
|
11
|
+
__publicField$1(this, "rawPath");
|
|
12
|
+
__publicField$1(this, "params");
|
|
22
13
|
this.rawPath = rawPath;
|
|
23
14
|
this.params = params;
|
|
24
15
|
}
|
|
25
16
|
buildUrl() {
|
|
26
|
-
return
|
|
17
|
+
return OpenAPIPath.setPathParams(this.rawPath, this.params);
|
|
27
18
|
}
|
|
28
19
|
static setPathParams(path, params) {
|
|
29
20
|
const asEntries = Object.entries(params ?? {});
|
|
@@ -33,12 +24,19 @@ var OpenAPIPath = class _OpenAPIPath {
|
|
|
33
24
|
}, path);
|
|
34
25
|
return finalPath.startsWith("/") ? finalPath.substring(1) : finalPath;
|
|
35
26
|
}
|
|
36
|
-
}
|
|
37
|
-
var OpenAPIPath_default = OpenAPIPath;
|
|
27
|
+
}
|
|
38
28
|
|
|
39
|
-
|
|
40
|
-
var
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
31
|
+
var __publicField = (obj, key, value) => {
|
|
32
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
35
|
+
class Request {
|
|
41
36
|
constructor(operationDescriptor, requestObject) {
|
|
37
|
+
__publicField(this, "operationDescriptor");
|
|
38
|
+
__publicField(this, "requestObject");
|
|
39
|
+
__publicField(this, "requestConfig");
|
|
42
40
|
this.operationDescriptor = operationDescriptor;
|
|
43
41
|
this.requestObject = requestObject;
|
|
44
42
|
this.requestConfig = Object.freeze(this.buildAxiosConfig());
|
|
@@ -49,7 +47,7 @@ var Request = class {
|
|
|
49
47
|
buildAxiosConfig() {
|
|
50
48
|
const { method, path } = this.operationDescriptor;
|
|
51
49
|
const pathParameters = this.requestObject;
|
|
52
|
-
const openApiPath = new
|
|
50
|
+
const openApiPath = new OpenAPIPath(path, pathParameters);
|
|
53
51
|
const url = openApiPath.buildUrl();
|
|
54
52
|
const data = this.requestObject && "data" in this.requestObject ? this.requestObject.data : void 0;
|
|
55
53
|
const headersConfig = this.requestObject && "headers" in this.requestObject ? this.requestObject.headers : void 0;
|
|
@@ -99,19 +97,16 @@ var Request = class {
|
|
|
99
97
|
}
|
|
100
98
|
throw new Error(`Unexpected query parameter type (${typeof query})`);
|
|
101
99
|
}
|
|
102
|
-
}
|
|
103
|
-
var Request_default = Request;
|
|
100
|
+
}
|
|
104
101
|
|
|
105
|
-
|
|
106
|
-
import { AxiosError } from "axios";
|
|
107
|
-
var ApiClientError = class _ApiClientError extends AxiosError {
|
|
102
|
+
class ApiClientError extends AxiosError {
|
|
108
103
|
constructor(message, code, config, request, response) {
|
|
109
104
|
super(message, code, config, request, response);
|
|
110
|
-
Object.setPrototypeOf(this,
|
|
105
|
+
Object.setPrototypeOf(this, ApiClientError.prototype);
|
|
111
106
|
this.name = "ApiClientError";
|
|
112
107
|
}
|
|
113
108
|
static fromResponse(message, response) {
|
|
114
|
-
return new
|
|
109
|
+
return new ApiClientError(
|
|
115
110
|
message,
|
|
116
111
|
void 0,
|
|
117
112
|
response.config,
|
|
@@ -119,36 +114,15 @@ var ApiClientError = class _ApiClientError extends AxiosError {
|
|
|
119
114
|
response
|
|
120
115
|
);
|
|
121
116
|
}
|
|
122
|
-
}
|
|
123
|
-
var ApiClientError_default = ApiClientError;
|
|
117
|
+
}
|
|
124
118
|
|
|
125
|
-
// src/types/assertStatus.ts
|
|
126
119
|
function assertStatus(response, expectedStatus) {
|
|
127
120
|
if (response.status !== expectedStatus) {
|
|
128
|
-
throw
|
|
129
|
-
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
|
|
130
|
-
response
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// src/types/assertOneOfStatus.ts
|
|
136
|
-
function assertOneOfStatus(response, expectedStatus) {
|
|
137
|
-
if (!expectedStatus.includes(response.status)) {
|
|
138
|
-
throw ApiClientError_default.fromResponse(
|
|
121
|
+
throw ApiClientError.fromResponse(
|
|
139
122
|
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
|
|
140
123
|
response
|
|
141
124
|
);
|
|
142
125
|
}
|
|
143
126
|
}
|
|
144
127
|
|
|
145
|
-
export {
|
|
146
|
-
__export,
|
|
147
|
-
__reExport,
|
|
148
|
-
OpenAPIPath,
|
|
149
|
-
Request,
|
|
150
|
-
Request_default,
|
|
151
|
-
ApiClientError,
|
|
152
|
-
assertStatus,
|
|
153
|
-
assertOneOfStatus
|
|
154
|
-
};
|
|
128
|
+
export { ApiClientError as A, OpenAPIPath as O, Request as R, assertStatus as a };
|
|
@@ -47,12 +47,16 @@ interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestType, Ign
|
|
|
47
47
|
}
|
|
48
48
|
type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
|
|
49
49
|
type InferredResponseType<TOp> = TOp extends OpenAPIOperation<RequestType, infer TRes> ? TRes : never;
|
|
50
|
-
type
|
|
50
|
+
type InferredResponseData<TOp, TStatus extends HttpStatus = 200> = Extract<InferredResponseType<TOp>, {
|
|
51
51
|
status: TStatus;
|
|
52
52
|
}>["data"];
|
|
53
|
-
type
|
|
53
|
+
type InferredRequestData<TOp> = TOp extends OpenAPIOperation ? InferredRequestType<TOp> extends {
|
|
54
54
|
data: infer TData;
|
|
55
55
|
} ? TData : never : never;
|
|
56
|
+
/** @deprecated Use InferredResponseData */
|
|
57
|
+
type ResponseData<TOp> = InferredResponseData<TOp>;
|
|
58
|
+
/** @deprecated Use InferredRequestData */
|
|
59
|
+
type RequestData<TOp> = InferredRequestData<TOp>;
|
|
56
60
|
|
|
57
61
|
type PartialOnNoRequiredKeysDeep<T> = PartialOnUndefinedDeep<OmitIndexSignature<{
|
|
58
62
|
[TKey in keyof T]: HasRequiredKeys<PartialOnNoRequiredKeysDeep<T[TKey]>> extends true ? T[TKey] : T[TKey] | undefined;
|
|
@@ -68,4 +72,4 @@ type RequestFunctionWithOptionalRequest<TOp extends OpenAPIOperation> = (request
|
|
|
68
72
|
type RequestFunctionWithRequiredRequest<TOp extends OpenAPIOperation> = (request: RequestObject<TOp>) => ResponsePromise<TOp>;
|
|
69
73
|
type RequestFunction<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? RequestFunctionWithOptionalRequest<TOp> : RequestFunctionWithRequiredRequest<TOp>;
|
|
70
74
|
|
|
71
|
-
export { AnyResponse as A, HttpPayload as H, InferredRequestType as I, OpenAPIOperation as O, PathParameters as P, QueryParameters as Q, Response as R, RequestFunction as a, RequestObject as b, ResponsePromise as c, RequestType as d, AnyRequest as e, InferredResponseType as f,
|
|
75
|
+
export type { AnyResponse as A, HttpPayload as H, InferredRequestType as I, OpenAPIOperation as O, PathParameters as P, QueryParameters as Q, Response as R, RequestFunction as a, RequestObject as b, ResponsePromise as c, RequestType as d, AnyRequest as e, InferredResponseType as f, InferredResponseData as g, InferredRequestData as h, ResponseData as i, RequestData as j, HttpStatus as k, HttpMediaType as l, HttpMethod as m, HttpHeaders as n };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
import { HasRequiredKeys, PartialOnUndefinedDeep, OmitIndexSignature } from 'type-fest';
|
|
3
|
+
|
|
4
|
+
type HttpPayload = unknown;
|
|
5
|
+
type HttpStatus = number | "default";
|
|
6
|
+
type HttpMediaType = string;
|
|
7
|
+
type SafeHttpMethod = "GET" | "HEAD" | "OPTIONS";
|
|
8
|
+
type UnsafeHttpMethod = "PUT" | "DELETE" | "POST" | "PATCH";
|
|
9
|
+
type HttpMethod = SafeHttpMethod | UnsafeHttpMethod;
|
|
10
|
+
type HeaderValue = string | number | boolean;
|
|
11
|
+
type HttpHeaders = Partial<{
|
|
12
|
+
[TKey: string]: HeaderValue | HeaderValue[];
|
|
13
|
+
}>;
|
|
14
|
+
type PathParameters = Record<string, string | number>;
|
|
15
|
+
type QueryParameters = Record<string, unknown>;
|
|
16
|
+
|
|
17
|
+
type EmptyObject = Record<string, never>;
|
|
18
|
+
type EmptyRequestComponent = EmptyObject | null;
|
|
19
|
+
type RequestWithOptionalHeaders = {
|
|
20
|
+
headers?: HttpHeaders;
|
|
21
|
+
};
|
|
22
|
+
type RequestWithData<TData> = TData extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
|
|
23
|
+
data: TData;
|
|
24
|
+
};
|
|
25
|
+
type RequestWithPathParameters<TPathParameters> = TPathParameters extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
|
|
26
|
+
pathParameters: TPathParameters;
|
|
27
|
+
};
|
|
28
|
+
type RequestWithHeaders<THeaders> = THeaders extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
|
|
29
|
+
headers: THeaders & HttpHeaders;
|
|
30
|
+
};
|
|
31
|
+
type RequestWithQueryParameters<TQuery> = TQuery extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
|
|
32
|
+
queryParameters: TQuery & HttpHeaders;
|
|
33
|
+
};
|
|
34
|
+
type RequestType<TData extends HttpPayload = EmptyRequestComponent, TPathParameters extends PathParameters | EmptyRequestComponent = EmptyRequestComponent, TQueryParameters extends QueryParameters | EmptyRequestComponent = EmptyRequestComponent, THeader extends HttpHeaders | EmptyRequestComponent = EmptyRequestComponent> = TData | TPathParameters | THeader | TQueryParameters extends EmptyRequestComponent ? RequestWithOptionalHeaders : RequestWithData<TData> & RequestWithPathParameters<TPathParameters> & RequestWithQueryParameters<TQueryParameters> & RequestWithHeaders<THeader>;
|
|
35
|
+
type AnyRequest = RequestType<any, any, any, any>;
|
|
36
|
+
|
|
37
|
+
type Response<TContent extends HttpPayload = HttpPayload, TStatus extends HttpStatus = HttpStatus, TMediaType extends HttpMediaType | null = HttpMediaType> = AxiosResponse<TContent> & {
|
|
38
|
+
status: TStatus;
|
|
39
|
+
mediaType: TMediaType;
|
|
40
|
+
};
|
|
41
|
+
type AnyResponse = Response<any, any, any>;
|
|
42
|
+
|
|
43
|
+
interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestType, IgnoredResponse extends AnyResponse = Response> {
|
|
44
|
+
operationId: string;
|
|
45
|
+
path: string;
|
|
46
|
+
method: HttpMethod;
|
|
47
|
+
}
|
|
48
|
+
type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
|
|
49
|
+
type InferredResponseType<TOp> = TOp extends OpenAPIOperation<RequestType, infer TRes> ? TRes : never;
|
|
50
|
+
type InferredResponseData<TOp, TStatus extends HttpStatus = 200> = Extract<InferredResponseType<TOp>, {
|
|
51
|
+
status: TStatus;
|
|
52
|
+
}>["data"];
|
|
53
|
+
type InferredRequestData<TOp> = TOp extends OpenAPIOperation ? InferredRequestType<TOp> extends {
|
|
54
|
+
data: infer TData;
|
|
55
|
+
} ? TData : never : never;
|
|
56
|
+
/** @deprecated Use InferredResponseData */
|
|
57
|
+
type ResponseData<TOp> = InferredResponseData<TOp>;
|
|
58
|
+
/** @deprecated Use InferredRequestData */
|
|
59
|
+
type RequestData<TOp> = InferredRequestData<TOp>;
|
|
60
|
+
|
|
61
|
+
type PartialOnNoRequiredKeysDeep<T> = PartialOnUndefinedDeep<OmitIndexSignature<{
|
|
62
|
+
[TKey in keyof T]: HasRequiredKeys<PartialOnNoRequiredKeysDeep<T[TKey]>> extends true ? T[TKey] : T[TKey] | undefined;
|
|
63
|
+
}>>;
|
|
64
|
+
type NullableOnNoRequiredKeysDeep<T> = HasRequiredKeys<PartialOnNoRequiredKeysDeep<T>> extends true ? PartialOnNoRequiredKeysDeep<T> : PartialOnNoRequiredKeysDeep<T> | null;
|
|
65
|
+
|
|
66
|
+
type UnboxPathParameters<T> = T extends {
|
|
67
|
+
pathParameters: infer TPath;
|
|
68
|
+
} ? Omit<T, "pathParameters"> & TPath : T;
|
|
69
|
+
type RequestObject<TOp extends OpenAPIOperation> = NullableOnNoRequiredKeysDeep<UnboxPathParameters<InferredRequestType<TOp>>>;
|
|
70
|
+
type ResponsePromise<TOp extends OpenAPIOperation> = Promise<InferredResponseType<TOp>>;
|
|
71
|
+
type RequestFunctionWithOptionalRequest<TOp extends OpenAPIOperation> = (request?: RequestObject<TOp>) => ResponsePromise<TOp>;
|
|
72
|
+
type RequestFunctionWithRequiredRequest<TOp extends OpenAPIOperation> = (request: RequestObject<TOp>) => ResponsePromise<TOp>;
|
|
73
|
+
type RequestFunction<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? RequestFunctionWithOptionalRequest<TOp> : RequestFunctionWithRequiredRequest<TOp>;
|
|
74
|
+
|
|
75
|
+
export type { AnyResponse as A, HttpPayload as H, InferredRequestType as I, OpenAPIOperation as O, PathParameters as P, QueryParameters as Q, Response as R, RequestFunction as a, RequestObject as b, ResponsePromise as c, RequestType as d, AnyRequest as e, InferredResponseType as f, InferredResponseData as g, InferredRequestData as h, ResponseData as i, RequestData as j, HttpStatus as k, HttpMediaType as l, HttpMethod as m, HttpHeaders as n };
|
package/package.json
CHANGED
|
@@ -1,68 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mittwald/api-client-commons",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.1",
|
|
4
|
+
"author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
|
|
5
|
+
"type": "module",
|
|
4
6
|
"description": "Common types and utilities for mittwald API clients",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
7
|
+
"keywords": [
|
|
8
|
+
"api",
|
|
9
|
+
"client",
|
|
10
|
+
"mittwald",
|
|
11
|
+
"rest",
|
|
12
|
+
"sdk"
|
|
13
|
+
],
|
|
11
14
|
"homepage": "https://developer.mittwald.de",
|
|
15
|
+
"repository": "https://github.com/mittwald/api-client-js.git",
|
|
12
16
|
"bugs": "https://github.com/mittwald/api-client-js/issues",
|
|
13
|
-
"
|
|
14
|
-
"module": "./dist/index.js",
|
|
15
|
-
"type": "module",
|
|
17
|
+
"license": "MIT",
|
|
16
18
|
"exports": {
|
|
17
19
|
".": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"types": "./dist/index.d.ts"
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"default": "./dist/index.mjs"
|
|
21
22
|
},
|
|
22
23
|
"./react": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"types": "./dist/react.d.ts"
|
|
24
|
+
"types": "./dist/react/index.d.mts",
|
|
25
|
+
"default": "./dist/react/index.mjs"
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"compile": "tsup src/index.ts src/react.ts --format esm,cjs --dts --clean",
|
|
30
|
-
"test": "node --experimental-vm-modules $(yarn bin jest)"
|
|
31
|
-
},
|
|
32
28
|
"files": [
|
|
33
|
-
"dist
|
|
29
|
+
"dist"
|
|
34
30
|
],
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"rest"
|
|
41
|
-
],
|
|
42
|
-
"peerDependencies": {
|
|
43
|
-
"@mittwald/react-use-promise": "^1.3.6"
|
|
44
|
-
},
|
|
45
|
-
"peerDependenciesMeta": {
|
|
46
|
-
"@mittwald/react-use-promise": {
|
|
47
|
-
"optional": true
|
|
48
|
-
}
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "yarn unbuild",
|
|
33
|
+
"format": "prettier --write '**/*.{ts,tsx,yaml,yml,json,md,mdx,js}'",
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"test": "node --experimental-vm-modules $(yarn bin jest)"
|
|
49
36
|
},
|
|
50
37
|
"dependencies": {
|
|
51
|
-
"@types/parse-path": "^7.0.
|
|
52
|
-
"axios": "^1.6.
|
|
38
|
+
"@types/parse-path": "^7.0.3",
|
|
39
|
+
"axios": "^1.6.2",
|
|
53
40
|
"parse-path": "^7.0.0",
|
|
54
41
|
"path-to-regexp": "^6.2.1",
|
|
55
|
-
"type-fest": "^4.
|
|
42
|
+
"type-fest": "^4.8.2"
|
|
56
43
|
},
|
|
57
44
|
"devDependencies": {
|
|
58
45
|
"@jest/globals": "^29.7.0",
|
|
59
|
-
"@mittwald/react-use-promise": "^1.
|
|
60
|
-
"@types/jest": "^29.5.
|
|
46
|
+
"@mittwald/react-use-promise": "^2.1.0",
|
|
47
|
+
"@types/jest": "^29.5.10",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.12.0",
|
|
49
|
+
"@typescript-eslint/parser": "^6.12.0",
|
|
50
|
+
"eslint": "^8.54.0",
|
|
51
|
+
"eslint-config-prettier": "^9.0.0",
|
|
52
|
+
"eslint-plugin-json": "^3.1.0",
|
|
53
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
61
54
|
"jest": "^29.7.0",
|
|
55
|
+
"prettier": "^3.1.0",
|
|
56
|
+
"prettier-plugin-jsdoc": "^1.1.1",
|
|
57
|
+
"prettier-plugin-pkgsort": "^0.2.1",
|
|
58
|
+
"prettier-plugin-sort-json": "^3.1.0",
|
|
62
59
|
"react": "^18.2.0",
|
|
63
60
|
"ts-jest": "^29.1.1",
|
|
64
|
-
"
|
|
65
|
-
"typescript": "^5.
|
|
61
|
+
"tsd": "^0.29.0",
|
|
62
|
+
"typescript": "^5.3.3",
|
|
63
|
+
"unbuild": "^2.0.0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@mittwald/react-use-promise": "^2.1.0"
|
|
67
|
+
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"@mittwald/react-use-promise": {
|
|
70
|
+
"optional": true
|
|
71
|
+
}
|
|
66
72
|
},
|
|
67
|
-
"
|
|
73
|
+
"unbuild": {
|
|
74
|
+
"declaration": true
|
|
75
|
+
}
|
|
68
76
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/index.ts
|
|
32
|
-
var src_exports = {};
|
|
33
|
-
__export(src_exports, {
|
|
34
|
-
ApiClientBase: () => ApiClientBase,
|
|
35
|
-
ApiClientError: () => ApiClientError,
|
|
36
|
-
OpenAPIPath: () => OpenAPIPath,
|
|
37
|
-
Request: () => Request,
|
|
38
|
-
assertOneOfStatus: () => assertOneOfStatus,
|
|
39
|
-
assertStatus: () => assertStatus
|
|
40
|
-
});
|
|
41
|
-
module.exports = __toCommonJS(src_exports);
|
|
42
|
-
|
|
43
|
-
// src/core/ApiClientBase.ts
|
|
44
|
-
var import_axios = __toESM(require("axios"), 1);
|
|
45
|
-
|
|
46
|
-
// src/core/OpenAPIPath.ts
|
|
47
|
-
var OpenAPIPath = class _OpenAPIPath {
|
|
48
|
-
constructor(rawPath, params) {
|
|
49
|
-
this.rawPath = rawPath;
|
|
50
|
-
this.params = params;
|
|
51
|
-
}
|
|
52
|
-
buildUrl() {
|
|
53
|
-
return _OpenAPIPath.setPathParams(this.rawPath, this.params);
|
|
54
|
-
}
|
|
55
|
-
static setPathParams(path, params) {
|
|
56
|
-
const asEntries = Object.entries(params ?? {});
|
|
57
|
-
const finalPath = asEntries.reduce((path2, entry) => {
|
|
58
|
-
const [key, value] = entry;
|
|
59
|
-
return path2.replace(`{${key}}`, encodeURIComponent(value));
|
|
60
|
-
}, path);
|
|
61
|
-
return finalPath.startsWith("/") ? finalPath.substring(1) : finalPath;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
var OpenAPIPath_default = OpenAPIPath;
|
|
65
|
-
|
|
66
|
-
// src/core/Request.ts
|
|
67
|
-
var Request = class {
|
|
68
|
-
constructor(operationDescriptor, requestObject) {
|
|
69
|
-
this.operationDescriptor = operationDescriptor;
|
|
70
|
-
this.requestObject = requestObject;
|
|
71
|
-
this.requestConfig = Object.freeze(this.buildAxiosConfig());
|
|
72
|
-
}
|
|
73
|
-
execute(axios2) {
|
|
74
|
-
return axios2.request(this.requestConfig);
|
|
75
|
-
}
|
|
76
|
-
buildAxiosConfig() {
|
|
77
|
-
const { method, path } = this.operationDescriptor;
|
|
78
|
-
const pathParameters = this.requestObject;
|
|
79
|
-
const openApiPath = new OpenAPIPath_default(path, pathParameters);
|
|
80
|
-
const url = openApiPath.buildUrl();
|
|
81
|
-
const data = this.requestObject && "data" in this.requestObject ? this.requestObject.data : void 0;
|
|
82
|
-
const headersConfig = this.requestObject && "headers" in this.requestObject ? this.requestObject.headers : void 0;
|
|
83
|
-
const headers = headersConfig ? this.makeAxiosHeaders(headersConfig) : void 0;
|
|
84
|
-
const queryParametersConfig = this.requestObject && "queryParameters" in this.requestObject ? this.requestObject.queryParameters : void 0;
|
|
85
|
-
const params = this.convertQueryToUrlSearchParams(queryParametersConfig);
|
|
86
|
-
return {
|
|
87
|
-
url,
|
|
88
|
-
method,
|
|
89
|
-
headers,
|
|
90
|
-
// Must be a plain object or an URLSearchParams object
|
|
91
|
-
params,
|
|
92
|
-
data,
|
|
93
|
-
validateStatus: () => true
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
makeAxiosHeaders(headers) {
|
|
97
|
-
return Object.fromEntries(
|
|
98
|
-
Object.entries(headers).map(([key, value]) => [key, value?.toString()])
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
convertQueryToUrlSearchParams(query) {
|
|
102
|
-
if (query === void 0 || query === null) {
|
|
103
|
-
return void 0;
|
|
104
|
-
}
|
|
105
|
-
if (query instanceof URLSearchParams) {
|
|
106
|
-
return query;
|
|
107
|
-
}
|
|
108
|
-
if (typeof query === "string") {
|
|
109
|
-
return new URLSearchParams(query);
|
|
110
|
-
}
|
|
111
|
-
if (typeof query === "object") {
|
|
112
|
-
const searchParams = new URLSearchParams();
|
|
113
|
-
for (const [key, value] of Object.entries(query)) {
|
|
114
|
-
if (Array.isArray(value)) {
|
|
115
|
-
for (const arrayItem of value) {
|
|
116
|
-
searchParams.append(key, arrayItem);
|
|
117
|
-
}
|
|
118
|
-
} else {
|
|
119
|
-
searchParams.append(
|
|
120
|
-
key,
|
|
121
|
-
typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? value.toString() : JSON.stringify(value)
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return searchParams;
|
|
126
|
-
}
|
|
127
|
-
throw new Error(`Unexpected query parameter type (${typeof query})`);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
var Request_default = Request;
|
|
131
|
-
|
|
132
|
-
// src/core/ApiClientBase.ts
|
|
133
|
-
var ApiClientBase = class {
|
|
134
|
-
constructor(axiosConfig = import_axios.default) {
|
|
135
|
-
this.axios = axiosConfig instanceof import_axios.Axios ? axiosConfig : import_axios.default.create(axiosConfig);
|
|
136
|
-
}
|
|
137
|
-
requestFunctionFactory(operation) {
|
|
138
|
-
return (conf) => new Request_default(operation, conf).execute(this.axios);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
// src/core/ApiClientError.ts
|
|
143
|
-
var import_axios2 = require("axios");
|
|
144
|
-
var ApiClientError = class _ApiClientError extends import_axios2.AxiosError {
|
|
145
|
-
constructor(message, code, config, request, response) {
|
|
146
|
-
super(message, code, config, request, response);
|
|
147
|
-
Object.setPrototypeOf(this, _ApiClientError.prototype);
|
|
148
|
-
this.name = "ApiClientError";
|
|
149
|
-
}
|
|
150
|
-
static fromResponse(message, response) {
|
|
151
|
-
return new _ApiClientError(
|
|
152
|
-
message,
|
|
153
|
-
void 0,
|
|
154
|
-
response.config,
|
|
155
|
-
response.request,
|
|
156
|
-
response
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
var ApiClientError_default = ApiClientError;
|
|
161
|
-
|
|
162
|
-
// src/types/assertStatus.ts
|
|
163
|
-
function assertStatus(response, expectedStatus) {
|
|
164
|
-
if (response.status !== expectedStatus) {
|
|
165
|
-
throw ApiClientError_default.fromResponse(
|
|
166
|
-
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
|
|
167
|
-
response
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// src/types/assertOneOfStatus.ts
|
|
173
|
-
function assertOneOfStatus(response, expectedStatus) {
|
|
174
|
-
if (!expectedStatus.includes(response.status)) {
|
|
175
|
-
throw ApiClientError_default.fromResponse(
|
|
176
|
-
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
|
|
177
|
-
response
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// src/axios.ts
|
|
183
|
-
var axios_exports = {};
|
|
184
|
-
__reExport(axios_exports, require("axios"));
|
|
185
|
-
|
|
186
|
-
// src/index.ts
|
|
187
|
-
__reExport(src_exports, axios_exports, module.exports);
|
|
188
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
189
|
-
0 && (module.exports = {
|
|
190
|
-
ApiClientBase,
|
|
191
|
-
ApiClientError,
|
|
192
|
-
OpenAPIPath,
|
|
193
|
-
Request,
|
|
194
|
-
assertOneOfStatus,
|
|
195
|
-
assertStatus
|
|
196
|
-
});
|
package/dist/index.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ApiClientError,
|
|
3
|
-
OpenAPIPath,
|
|
4
|
-
Request,
|
|
5
|
-
Request_default,
|
|
6
|
-
__export,
|
|
7
|
-
__reExport,
|
|
8
|
-
assertOneOfStatus,
|
|
9
|
-
assertStatus
|
|
10
|
-
} from "./chunk-PEYA5TMK.js";
|
|
11
|
-
|
|
12
|
-
// src/index.ts
|
|
13
|
-
var src_exports = {};
|
|
14
|
-
__export(src_exports, {
|
|
15
|
-
ApiClientBase: () => ApiClientBase,
|
|
16
|
-
ApiClientError: () => ApiClientError,
|
|
17
|
-
OpenAPIPath: () => OpenAPIPath,
|
|
18
|
-
Request: () => Request,
|
|
19
|
-
assertOneOfStatus: () => assertOneOfStatus,
|
|
20
|
-
assertStatus: () => assertStatus
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// src/core/ApiClientBase.ts
|
|
24
|
-
import axios, { Axios } from "axios";
|
|
25
|
-
var ApiClientBase = class {
|
|
26
|
-
constructor(axiosConfig = axios) {
|
|
27
|
-
this.axios = axiosConfig instanceof Axios ? axiosConfig : axios.create(axiosConfig);
|
|
28
|
-
}
|
|
29
|
-
requestFunctionFactory(operation) {
|
|
30
|
-
return (conf) => new Request_default(operation, conf).execute(this.axios);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// src/axios.ts
|
|
35
|
-
var axios_exports = {};
|
|
36
|
-
__reExport(axios_exports, axios_star);
|
|
37
|
-
import * as axios_star from "axios";
|
|
38
|
-
|
|
39
|
-
// src/index.ts
|
|
40
|
-
__reExport(src_exports, axios_exports);
|
|
41
|
-
export {
|
|
42
|
-
ApiClientBase,
|
|
43
|
-
ApiClientError,
|
|
44
|
-
OpenAPIPath,
|
|
45
|
-
Request,
|
|
46
|
-
assertOneOfStatus,
|
|
47
|
-
assertStatus
|
|
48
|
-
};
|
package/dist/react.cjs
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/react.ts
|
|
21
|
-
var react_exports = {};
|
|
22
|
-
__export(react_exports, {
|
|
23
|
-
ApiCallAsyncResourceFactory: () => ApiCallAsyncResourceFactory
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(react_exports);
|
|
26
|
-
|
|
27
|
-
// src/react/ApiCallAsyncResourceFactory.ts
|
|
28
|
-
var import_react_use_promise = require("@mittwald/react-use-promise");
|
|
29
|
-
|
|
30
|
-
// src/core/ApiClientError.ts
|
|
31
|
-
var import_axios = require("axios");
|
|
32
|
-
var ApiClientError = class _ApiClientError extends import_axios.AxiosError {
|
|
33
|
-
constructor(message, code, config, request, response) {
|
|
34
|
-
super(message, code, config, request, response);
|
|
35
|
-
Object.setPrototypeOf(this, _ApiClientError.prototype);
|
|
36
|
-
this.name = "ApiClientError";
|
|
37
|
-
}
|
|
38
|
-
static fromResponse(message, response) {
|
|
39
|
-
return new _ApiClientError(
|
|
40
|
-
message,
|
|
41
|
-
void 0,
|
|
42
|
-
response.config,
|
|
43
|
-
response.request,
|
|
44
|
-
response
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var ApiClientError_default = ApiClientError;
|
|
49
|
-
|
|
50
|
-
// src/types/assertStatus.ts
|
|
51
|
-
function assertStatus(response, expectedStatus) {
|
|
52
|
-
if (response.status !== expectedStatus) {
|
|
53
|
-
throw ApiClientError_default.fromResponse(
|
|
54
|
-
`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
|
|
55
|
-
response
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/core/OpenAPIPath.ts
|
|
61
|
-
var OpenAPIPath = class _OpenAPIPath {
|
|
62
|
-
constructor(rawPath, params) {
|
|
63
|
-
this.rawPath = rawPath;
|
|
64
|
-
this.params = params;
|
|
65
|
-
}
|
|
66
|
-
buildUrl() {
|
|
67
|
-
return _OpenAPIPath.setPathParams(this.rawPath, this.params);
|
|
68
|
-
}
|
|
69
|
-
static setPathParams(path, params) {
|
|
70
|
-
const asEntries = Object.entries(params ?? {});
|
|
71
|
-
const finalPath = asEntries.reduce((path2, entry) => {
|
|
72
|
-
const [key, value] = entry;
|
|
73
|
-
return path2.replace(`{${key}}`, encodeURIComponent(value));
|
|
74
|
-
}, path);
|
|
75
|
-
return finalPath.startsWith("/") ? finalPath.substring(1) : finalPath;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
var OpenAPIPath_default = OpenAPIPath;
|
|
79
|
-
|
|
80
|
-
// src/core/Request.ts
|
|
81
|
-
var Request = class {
|
|
82
|
-
constructor(operationDescriptor, requestObject) {
|
|
83
|
-
this.operationDescriptor = operationDescriptor;
|
|
84
|
-
this.requestObject = requestObject;
|
|
85
|
-
this.requestConfig = Object.freeze(this.buildAxiosConfig());
|
|
86
|
-
}
|
|
87
|
-
execute(axios) {
|
|
88
|
-
return axios.request(this.requestConfig);
|
|
89
|
-
}
|
|
90
|
-
buildAxiosConfig() {
|
|
91
|
-
const { method, path } = this.operationDescriptor;
|
|
92
|
-
const pathParameters = this.requestObject;
|
|
93
|
-
const openApiPath = new OpenAPIPath_default(path, pathParameters);
|
|
94
|
-
const url = openApiPath.buildUrl();
|
|
95
|
-
const data = this.requestObject && "data" in this.requestObject ? this.requestObject.data : void 0;
|
|
96
|
-
const headersConfig = this.requestObject && "headers" in this.requestObject ? this.requestObject.headers : void 0;
|
|
97
|
-
const headers = headersConfig ? this.makeAxiosHeaders(headersConfig) : void 0;
|
|
98
|
-
const queryParametersConfig = this.requestObject && "queryParameters" in this.requestObject ? this.requestObject.queryParameters : void 0;
|
|
99
|
-
const params = this.convertQueryToUrlSearchParams(queryParametersConfig);
|
|
100
|
-
return {
|
|
101
|
-
url,
|
|
102
|
-
method,
|
|
103
|
-
headers,
|
|
104
|
-
// Must be a plain object or an URLSearchParams object
|
|
105
|
-
params,
|
|
106
|
-
data,
|
|
107
|
-
validateStatus: () => true
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
makeAxiosHeaders(headers) {
|
|
111
|
-
return Object.fromEntries(
|
|
112
|
-
Object.entries(headers).map(([key, value]) => [key, value?.toString()])
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
convertQueryToUrlSearchParams(query) {
|
|
116
|
-
if (query === void 0 || query === null) {
|
|
117
|
-
return void 0;
|
|
118
|
-
}
|
|
119
|
-
if (query instanceof URLSearchParams) {
|
|
120
|
-
return query;
|
|
121
|
-
}
|
|
122
|
-
if (typeof query === "string") {
|
|
123
|
-
return new URLSearchParams(query);
|
|
124
|
-
}
|
|
125
|
-
if (typeof query === "object") {
|
|
126
|
-
const searchParams = new URLSearchParams();
|
|
127
|
-
for (const [key, value] of Object.entries(query)) {
|
|
128
|
-
if (Array.isArray(value)) {
|
|
129
|
-
for (const arrayItem of value) {
|
|
130
|
-
searchParams.append(key, arrayItem);
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
searchParams.append(
|
|
134
|
-
key,
|
|
135
|
-
typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? value.toString() : JSON.stringify(value)
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return searchParams;
|
|
140
|
-
}
|
|
141
|
-
throw new Error(`Unexpected query parameter type (${typeof query})`);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
var Request_default = Request;
|
|
145
|
-
|
|
146
|
-
// src/react/ApiCallAsyncResourceFactory.ts
|
|
147
|
-
var _ApiCallAsyncResourceFactory = class _ApiCallAsyncResourceFactory {
|
|
148
|
-
constructor(operation, requestFn) {
|
|
149
|
-
this.getApiResource = (requestObj) => {
|
|
150
|
-
const request = new Request_default(this.operation, requestObj);
|
|
151
|
-
return (0, import_react_use_promise.getAsyncResource)(
|
|
152
|
-
(requestObj2) => this.executeRequest(requestObj2),
|
|
153
|
-
[requestObj],
|
|
154
|
-
{
|
|
155
|
-
tags: this.getAsyncResourceTags(request),
|
|
156
|
-
loaderId: this.getAsyncResourceId()
|
|
157
|
-
}
|
|
158
|
-
);
|
|
159
|
-
};
|
|
160
|
-
this.operation = operation;
|
|
161
|
-
this.requestFn = requestFn;
|
|
162
|
-
}
|
|
163
|
-
getAsyncResourceId() {
|
|
164
|
-
return `${_ApiCallAsyncResourceFactory.namespace}/${this.operation.operationId}`;
|
|
165
|
-
}
|
|
166
|
-
getAsyncResourceTags(request) {
|
|
167
|
-
const url = request.requestConfig.url ?? "";
|
|
168
|
-
return [
|
|
169
|
-
this.getAsyncResourceId(),
|
|
170
|
-
`${_ApiCallAsyncResourceFactory.namespace}/${this.operation.method}`,
|
|
171
|
-
`${_ApiCallAsyncResourceFactory.namespace}/${url}`
|
|
172
|
-
];
|
|
173
|
-
}
|
|
174
|
-
async executeRequest(requestObj) {
|
|
175
|
-
const response = await this.requestFn(requestObj);
|
|
176
|
-
assertStatus(response, 200);
|
|
177
|
-
return response.data;
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
_ApiCallAsyncResourceFactory.namespace = "@mittwald/api-client";
|
|
181
|
-
var ApiCallAsyncResourceFactory = _ApiCallAsyncResourceFactory;
|
|
182
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
183
|
-
0 && (module.exports = {
|
|
184
|
-
ApiCallAsyncResourceFactory
|
|
185
|
-
});
|
package/dist/react.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Request_default,
|
|
3
|
-
assertStatus
|
|
4
|
-
} from "./chunk-PEYA5TMK.js";
|
|
5
|
-
|
|
6
|
-
// src/react/ApiCallAsyncResourceFactory.ts
|
|
7
|
-
import { getAsyncResource } from "@mittwald/react-use-promise";
|
|
8
|
-
var _ApiCallAsyncResourceFactory = class _ApiCallAsyncResourceFactory {
|
|
9
|
-
constructor(operation, requestFn) {
|
|
10
|
-
this.getApiResource = (requestObj) => {
|
|
11
|
-
const request = new Request_default(this.operation, requestObj);
|
|
12
|
-
return getAsyncResource(
|
|
13
|
-
(requestObj2) => this.executeRequest(requestObj2),
|
|
14
|
-
[requestObj],
|
|
15
|
-
{
|
|
16
|
-
tags: this.getAsyncResourceTags(request),
|
|
17
|
-
loaderId: this.getAsyncResourceId()
|
|
18
|
-
}
|
|
19
|
-
);
|
|
20
|
-
};
|
|
21
|
-
this.operation = operation;
|
|
22
|
-
this.requestFn = requestFn;
|
|
23
|
-
}
|
|
24
|
-
getAsyncResourceId() {
|
|
25
|
-
return `${_ApiCallAsyncResourceFactory.namespace}/${this.operation.operationId}`;
|
|
26
|
-
}
|
|
27
|
-
getAsyncResourceTags(request) {
|
|
28
|
-
const url = request.requestConfig.url ?? "";
|
|
29
|
-
return [
|
|
30
|
-
this.getAsyncResourceId(),
|
|
31
|
-
`${_ApiCallAsyncResourceFactory.namespace}/${this.operation.method}`,
|
|
32
|
-
`${_ApiCallAsyncResourceFactory.namespace}/${url}`
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
async executeRequest(requestObj) {
|
|
36
|
-
const response = await this.requestFn(requestObj);
|
|
37
|
-
assertStatus(response, 200);
|
|
38
|
-
return response.data;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
_ApiCallAsyncResourceFactory.namespace = "@mittwald/api-client";
|
|
42
|
-
var ApiCallAsyncResourceFactory = _ApiCallAsyncResourceFactory;
|
|
43
|
-
export {
|
|
44
|
-
ApiCallAsyncResourceFactory
|
|
45
|
-
};
|