@mittwald/api-client-commons 4.3.0 → 4.3.2

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.
Files changed (62) hide show
  1. package/dist/esm/axios.js +1 -0
  2. package/dist/esm/core/ApiClientBase.js +13 -0
  3. package/dist/esm/core/ApiClientError.js +12 -0
  4. package/dist/esm/core/OpenAPIPath.js +20 -0
  5. package/dist/esm/core/Request.js +76 -0
  6. package/dist/esm/core/Request.test.js +53 -0
  7. package/dist/esm/core/index.js +4 -0
  8. package/dist/esm/index.js +3 -0
  9. package/dist/esm/react/ApiCallAsyncResourceFactory.js +35 -0
  10. package/dist/esm/react/ApiCallAsyncResourceFactory.test-types.js +36 -0
  11. package/dist/esm/react/ApiCallAsyncResourceFactory.test.js +46 -0
  12. package/dist/esm/react/index.js +1 -0
  13. package/dist/esm/react/types.js +1 -0
  14. package/dist/esm/types/NullableOnNoRequiredKeysDeep.js +1 -0
  15. package/dist/esm/types/OpenAPIOperation.js +1 -0
  16. package/dist/esm/types/RequestFunction.js +1 -0
  17. package/dist/esm/types/RequestFunction.test-types.js +29 -0
  18. package/dist/esm/types/RequestType.js +1 -0
  19. package/dist/esm/types/RequestType.test-types.js +146 -0
  20. package/dist/esm/types/Response.js +1 -0
  21. package/dist/esm/types/Response.test-types.js +70 -0
  22. package/dist/esm/types/assertStatus.js +7 -0
  23. package/dist/esm/types/assertStatus.test-types.js +8 -0
  24. package/dist/esm/types/http.js +1 -0
  25. package/dist/esm/types/index.js +7 -0
  26. package/dist/esm/types/simplify.js +1 -0
  27. package/dist/types/axios.d.ts +1 -0
  28. package/dist/types/core/ApiClientBase.d.ts +9 -0
  29. package/dist/types/core/ApiClientError.d.ts +7 -0
  30. package/dist/types/core/OpenAPIPath.d.ts +9 -0
  31. package/dist/types/core/Request.d.ts +13 -0
  32. package/dist/types/core/Request.test.d.ts +1 -0
  33. package/dist/types/core/index.d.ts +4 -0
  34. package/dist/types/index.d.ts +3 -0
  35. package/dist/types/react/ApiCallAsyncResourceFactory.d.ts +12 -0
  36. package/dist/types/react/ApiCallAsyncResourceFactory.test-types.d.ts +1 -0
  37. package/dist/types/react/ApiCallAsyncResourceFactory.test.d.ts +1 -0
  38. package/dist/types/react/index.d.ts +1 -0
  39. package/dist/types/react/types.d.ts +3 -0
  40. package/dist/types/types/NullableOnNoRequiredKeysDeep.d.ts +6 -0
  41. package/dist/types/types/OpenAPIOperation.d.ts +20 -0
  42. package/dist/types/types/RequestFunction.d.ts +11 -0
  43. package/dist/types/types/RequestFunction.test-types.d.ts +1 -0
  44. package/dist/types/types/RequestType.d.ts +21 -0
  45. package/dist/types/types/RequestType.test-types.d.ts +1 -0
  46. package/dist/types/types/Response.d.ts +7 -0
  47. package/dist/types/types/Response.test-types.d.ts +1 -0
  48. package/dist/types/types/assertStatus.d.ts +5 -0
  49. package/dist/types/types/assertStatus.test-types.d.ts +1 -0
  50. package/dist/types/types/http.d.ts +13 -0
  51. package/dist/types/types/index.d.ts +7 -0
  52. package/dist/types/types/simplify.d.ts +3 -0
  53. package/package.json +25 -27
  54. package/dist/index.d.mts +0 -45
  55. package/dist/index.d.ts +0 -45
  56. package/dist/index.mjs +0 -22
  57. package/dist/react/index.d.mts +0 -19
  58. package/dist/react/index.d.ts +0 -19
  59. package/dist/react/index.mjs +0 -49
  60. package/dist/shared/api-client-commons.2a6e3962.mjs +0 -128
  61. package/dist/shared/api-client-commons.8b686095.d.mts +0 -75
  62. package/dist/shared/api-client-commons.8b686095.d.ts +0 -75
@@ -0,0 +1,3 @@
1
+ export * from "./core/index.js";
2
+ export * from "./types/index.js";
3
+ export * from "./axios.js";
@@ -0,0 +1,12 @@
1
+ import { OpenAPIOperation, RequestFunction } from "../types/index.js";
2
+ import { GetApiResourceFn } from "./types.js";
3
+ export declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
4
+ private static namespace;
5
+ private readonly operation;
6
+ private readonly requestFn;
7
+ constructor(operation: TOp, requestFn: RequestFunction<TOp>);
8
+ private getAsyncResourceId;
9
+ private getAsyncResourceTags;
10
+ private executeRequest;
11
+ getApiResource: GetApiResourceFn<TOp>;
12
+ }
@@ -0,0 +1 @@
1
+ export * from "./ApiCallAsyncResourceFactory.js";
@@ -0,0 +1,3 @@
1
+ import { OpenAPIOperation, RequestObject, InferredResponseData } from "../types/index.js";
2
+ import { AsyncResource } from "@mittwald/react-use-promise";
3
+ export type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>>;
@@ -0,0 +1,6 @@
1
+ import { HasRequiredKeys, OmitIndexSignature, PartialOnUndefinedDeep } from "type-fest";
2
+ type PartialOnNoRequiredKeysDeep<T> = PartialOnUndefinedDeep<OmitIndexSignature<{
3
+ [TKey in keyof T]: HasRequiredKeys<PartialOnNoRequiredKeysDeep<T[TKey]>> extends true ? T[TKey] : T[TKey] | undefined;
4
+ }>>;
5
+ export type NullableOnNoRequiredKeysDeep<T> = HasRequiredKeys<PartialOnNoRequiredKeysDeep<T>> extends true ? PartialOnNoRequiredKeysDeep<T> : PartialOnNoRequiredKeysDeep<T> | null;
6
+ export {};
@@ -0,0 +1,20 @@
1
+ import { AnyResponse, Response } from "./Response.js";
2
+ import { AnyRequest, RequestType } from "./RequestType.js";
3
+ import { HttpMethod, HttpStatus } from "./http.js";
4
+ export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestType, IgnoredResponse extends AnyResponse = Response> {
5
+ operationId: string;
6
+ path: string;
7
+ method: HttpMethod;
8
+ }
9
+ export type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
10
+ export type InferredResponseType<TOp> = TOp extends OpenAPIOperation<RequestType, infer TRes> ? TRes : never;
11
+ export type InferredResponseData<TOp, TStatus extends HttpStatus = 200> = Extract<InferredResponseType<TOp>, {
12
+ status: TStatus;
13
+ }>["data"];
14
+ export type InferredRequestData<TOp> = TOp extends OpenAPIOperation ? InferredRequestType<TOp> extends {
15
+ data: infer TData;
16
+ } ? TData : never : never;
17
+ /** @deprecated Use InferredResponseData */
18
+ export type ResponseData<TOp> = InferredResponseData<TOp>;
19
+ /** @deprecated Use InferredRequestData */
20
+ export type RequestData<TOp> = InferredRequestData<TOp>;
@@ -0,0 +1,11 @@
1
+ import { InferredRequestType, InferredResponseType, OpenAPIOperation } from "./OpenAPIOperation.js";
2
+ import { NullableOnNoRequiredKeysDeep } from "./NullableOnNoRequiredKeysDeep.js";
3
+ type UnboxPathParameters<T> = T extends {
4
+ pathParameters: infer TPath;
5
+ } ? Omit<T, "pathParameters"> & TPath : T;
6
+ export type RequestObject<TOp extends OpenAPIOperation> = NullableOnNoRequiredKeysDeep<UnboxPathParameters<InferredRequestType<TOp>>>;
7
+ export type ResponsePromise<TOp extends OpenAPIOperation> = Promise<InferredResponseType<TOp>>;
8
+ type RequestFunctionWithOptionalRequest<TOp extends OpenAPIOperation> = (request?: RequestObject<TOp>) => ResponsePromise<TOp>;
9
+ type RequestFunctionWithRequiredRequest<TOp extends OpenAPIOperation> = (request: RequestObject<TOp>) => ResponsePromise<TOp>;
10
+ export type RequestFunction<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? RequestFunctionWithOptionalRequest<TOp> : RequestFunctionWithRequiredRequest<TOp>;
11
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import { HttpHeaders, HttpPayload, PathParameters, QueryParameters } from "./http.js";
2
+ type EmptyObject = Record<string, never>;
3
+ type EmptyRequestComponent = EmptyObject | null;
4
+ type RequestWithOptionalHeaders = {
5
+ headers?: HttpHeaders;
6
+ };
7
+ type RequestWithData<TData> = TData extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
8
+ data: TData;
9
+ };
10
+ type RequestWithPathParameters<TPathParameters> = TPathParameters extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
11
+ pathParameters: TPathParameters;
12
+ };
13
+ type RequestWithHeaders<THeaders> = THeaders extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
14
+ headers: THeaders & HttpHeaders;
15
+ };
16
+ type RequestWithQueryParameters<TQuery> = TQuery extends EmptyRequestComponent ? RequestWithOptionalHeaders : {
17
+ queryParameters: TQuery & HttpHeaders;
18
+ };
19
+ export 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>;
20
+ export type AnyRequest = RequestType<any, any, any, any>;
21
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { HttpMediaType, HttpPayload, HttpStatus } from "./http.js";
2
+ import { AxiosResponse } from "axios";
3
+ export type Response<TContent extends HttpPayload = HttpPayload, TStatus extends HttpStatus = HttpStatus, TMediaType extends HttpMediaType | null = HttpMediaType> = AxiosResponse<TContent> & {
4
+ status: TStatus;
5
+ mediaType: TMediaType;
6
+ };
7
+ export type AnyResponse = Response<any, any, any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Response } from "./Response.js";
2
+ export declare function assertStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S): asserts response is T & {
3
+ status: S;
4
+ };
5
+ export default assertStatus;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ export type HttpPayload = unknown;
2
+ export type HttpStatus = number | "default";
3
+ export type HttpMediaType = string;
4
+ type SafeHttpMethod = "GET" | "HEAD" | "OPTIONS";
5
+ type UnsafeHttpMethod = "PUT" | "DELETE" | "POST" | "PATCH";
6
+ export type HttpMethod = SafeHttpMethod | UnsafeHttpMethod;
7
+ type HeaderValue = string | number | boolean;
8
+ export type HttpHeaders = Partial<{
9
+ [TKey: string]: HeaderValue | HeaderValue[];
10
+ }>;
11
+ export type PathParameters = Record<string, string | number>;
12
+ export type QueryParameters = Record<string, unknown>;
13
+ export {};
@@ -0,0 +1,7 @@
1
+ export * from "./RequestType.js";
2
+ export * from "./RequestFunction.js";
3
+ export * from "./Response.js";
4
+ export * from "./OpenAPIOperation.js";
5
+ export * from "./http.js";
6
+ export * from "./simplify.js";
7
+ export * from "./assertStatus.js";
@@ -0,0 +1,3 @@
1
+ export type Simplify<T> = {
2
+ [KeyType in keyof T]: T[KeyType];
3
+ } & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/api-client-commons",
3
- "version": "4.3.0",
3
+ "version": "4.3.2",
4
4
  "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
5
  "type": "module",
6
6
  "description": "Common types and utilities for mittwald API clients",
@@ -17,50 +17,51 @@
17
17
  "license": "MIT",
18
18
  "exports": {
19
19
  ".": {
20
- "types": "./dist/index.d.mts",
21
- "default": "./dist/index.mjs"
20
+ "types": "./dist/types/index.d.ts",
21
+ "default": "./dist/esm/index.js"
22
22
  },
23
23
  "./react": {
24
- "types": "./dist/react/index.d.mts",
25
- "default": "./dist/react/index.mjs"
24
+ "types": "./dist/types/react/index.d.ts",
25
+ "default": "./dist/esm/react/index.js"
26
26
  }
27
27
  },
28
28
  "files": [
29
29
  "dist"
30
30
  ],
31
31
  "scripts": {
32
- "build": "yarn unbuild",
33
- "format": "prettier --write '**/*.{ts,tsx,yaml,yml,json,md,mdx,js}'",
34
- "lint": "eslint .",
32
+ "build": "run build:clean && run tsc",
33
+ "build:clean": "rimraf dist",
34
+ "format": "run prettier --write '**/*.{ts,tsx,yaml,yml,json,md,mdx,js}'",
35
+ "lint": "run eslint .",
35
36
  "test": "node --experimental-vm-modules $(yarn bin jest)"
36
37
  },
37
38
  "dependencies": {
38
39
  "@types/parse-path": "^7.0.3",
39
- "axios": "^1.6.2",
40
+ "axios": "^1.6.7",
40
41
  "parse-path": "^7.0.0",
41
- "path-to-regexp": "^6.2.1",
42
- "type-fest": "^4.8.2"
42
+ "path-to-regexp": "^8.1.0",
43
+ "type-fest": "^4.10.3"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@jest/globals": "^29.7.0",
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",
47
+ "@mittwald/react-use-promise": "^2.3.12",
48
+ "@types/jest": "^29.5.12",
49
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
50
+ "@typescript-eslint/parser": "^7.0.2",
51
+ "eslint": "^8.56.0",
52
+ "eslint-config-prettier": "^9.1.0",
52
53
  "eslint-plugin-json": "^3.1.0",
53
- "eslint-plugin-prettier": "^5.0.1",
54
+ "eslint-plugin-prettier": "^5.1.3",
54
55
  "jest": "^29.7.0",
55
- "prettier": "^3.1.0",
56
- "prettier-plugin-jsdoc": "^1.1.1",
56
+ "prettier": "^3.2.5",
57
+ "prettier-plugin-jsdoc": "^1.3.0",
57
58
  "prettier-plugin-pkgsort": "^0.2.1",
58
59
  "prettier-plugin-sort-json": "^3.1.0",
59
60
  "react": "^18.2.0",
60
- "ts-jest": "^29.1.1",
61
- "tsd": "^0.29.0",
62
- "typescript": "^5.3.3",
63
- "unbuild": "^2.0.0"
61
+ "rimraf": "^5.0.5",
62
+ "ts-jest": "^29.1.2",
63
+ "tsd": "^0.30.5",
64
+ "typescript": "^5.3.3"
64
65
  },
65
66
  "peerDependencies": {
66
67
  "@mittwald/react-use-promise": "^2.1.0"
@@ -69,8 +70,5 @@
69
70
  "@mittwald/react-use-promise": {
70
71
  "optional": true
71
72
  }
72
- },
73
- "unbuild": {
74
- "declaration": true
75
73
  }
76
74
  }
package/dist/index.d.mts DELETED
@@ -1,45 +0,0 @@
1
- import { AxiosInstance, CreateAxiosDefaults, AxiosError, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig } from 'axios';
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 './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
- import 'type-fest';
6
-
7
- type Simplify<T> = {
8
- [KeyType in keyof T]: T[KeyType];
9
- } & {};
10
-
11
- declare function assertStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S): asserts response is T & {
12
- status: S;
13
- };
14
-
15
- declare abstract class ApiClientBase {
16
- axios: AxiosInstance;
17
- constructor(axiosConfig?: AxiosInstance | CreateAxiosDefaults);
18
- protected requestFunctionFactory<TOp extends OpenAPIOperation>(operation: TOp): RequestFunction<TOp>;
19
- }
20
-
21
- declare class ApiClientError<T = unknown, D = unknown> extends AxiosError<T, D> {
22
- constructor(message?: string, code?: string, config?: InternalAxiosRequestConfig<D>, request?: unknown, response?: AxiosResponse<T, D>);
23
- static fromResponse(message: string, response: AnyResponse): ApiClientError;
24
- }
25
-
26
- declare class OpenAPIPath {
27
- private readonly rawPath;
28
- private readonly params?;
29
- constructor(rawPath: string, params?: PathParameters);
30
- buildUrl(): string;
31
- private static setPathParams;
32
- }
33
-
34
- declare class Request<TOp extends OpenAPIOperation> {
35
- private readonly operationDescriptor;
36
- private readonly requestObject?;
37
- readonly requestConfig: AxiosRequestConfig;
38
- constructor(operationDescriptor: TOp, requestObject?: RequestObject<TOp>);
39
- execute(axios: AxiosInstance): ResponsePromise<TOp>;
40
- private buildAxiosConfig;
41
- private makeAxiosHeaders;
42
- private convertQueryToUrlSearchParams;
43
- }
44
-
45
- export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, type Simplify, assertStatus };
package/dist/index.d.ts DELETED
@@ -1,45 +0,0 @@
1
- import { AxiosInstance, CreateAxiosDefaults, AxiosError, InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig } from 'axios';
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 './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
- import 'type-fest';
6
-
7
- type Simplify<T> = {
8
- [KeyType in keyof T]: T[KeyType];
9
- } & {};
10
-
11
- declare function assertStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S): asserts response is T & {
12
- status: S;
13
- };
14
-
15
- declare abstract class ApiClientBase {
16
- axios: AxiosInstance;
17
- constructor(axiosConfig?: AxiosInstance | CreateAxiosDefaults);
18
- protected requestFunctionFactory<TOp extends OpenAPIOperation>(operation: TOp): RequestFunction<TOp>;
19
- }
20
-
21
- declare class ApiClientError<T = unknown, D = unknown> extends AxiosError<T, D> {
22
- constructor(message?: string, code?: string, config?: InternalAxiosRequestConfig<D>, request?: unknown, response?: AxiosResponse<T, D>);
23
- static fromResponse(message: string, response: AnyResponse): ApiClientError;
24
- }
25
-
26
- declare class OpenAPIPath {
27
- private readonly rawPath;
28
- private readonly params?;
29
- constructor(rawPath: string, params?: PathParameters);
30
- buildUrl(): string;
31
- private static setPathParams;
32
- }
33
-
34
- declare class Request<TOp extends OpenAPIOperation> {
35
- private readonly operationDescriptor;
36
- private readonly requestObject?;
37
- readonly requestConfig: AxiosRequestConfig;
38
- constructor(operationDescriptor: TOp, requestObject?: RequestObject<TOp>);
39
- execute(axios: AxiosInstance): ResponsePromise<TOp>;
40
- private buildAxiosConfig;
41
- private makeAxiosHeaders;
42
- private convertQueryToUrlSearchParams;
43
- }
44
-
45
- export { AnyResponse, ApiClientBase, ApiClientError, OpenAPIOperation, OpenAPIPath, PathParameters, Request, RequestFunction, RequestObject, Response, ResponsePromise, type Simplify, assertStatus };
package/dist/index.mjs DELETED
@@ -1,22 +0,0 @@
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,19 +0,0 @@
1
- import { O as OpenAPIOperation, b as RequestObject, g as InferredResponseData, a as RequestFunction } from '../shared/api-client-commons.8b686095.mjs';
2
- import { AsyncResource } from '@mittwald/react-use-promise';
3
- import 'axios';
4
- import 'type-fest';
5
-
6
- type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>>;
7
-
8
- declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
9
- private static namespace;
10
- private readonly operation;
11
- private readonly requestFn;
12
- constructor(operation: TOp, requestFn: RequestFunction<TOp>);
13
- private getAsyncResourceId;
14
- private getAsyncResourceTags;
15
- private executeRequest;
16
- getApiResource: GetApiResourceFn<TOp>;
17
- }
18
-
19
- export { ApiCallAsyncResourceFactory };
@@ -1,19 +0,0 @@
1
- import { O as OpenAPIOperation, b as RequestObject, g as InferredResponseData, a as RequestFunction } from '../shared/api-client-commons.8b686095.js';
2
- import { AsyncResource } from '@mittwald/react-use-promise';
3
- import 'axios';
4
- import 'type-fest';
5
-
6
- type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseData<TOp>>;
7
-
8
- declare class ApiCallAsyncResourceFactory<TOp extends OpenAPIOperation> {
9
- private static namespace;
10
- private readonly operation;
11
- private readonly requestFn;
12
- constructor(operation: TOp, requestFn: RequestFunction<TOp>);
13
- private getAsyncResourceId;
14
- private getAsyncResourceTags;
15
- private executeRequest;
16
- getApiResource: GetApiResourceFn<TOp>;
17
- }
18
-
19
- export { ApiCallAsyncResourceFactory };
@@ -1,49 +0,0 @@
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,128 +0,0 @@
1
- import { AxiosError } from 'axios';
2
-
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 {
10
- constructor(rawPath, params) {
11
- __publicField$1(this, "rawPath");
12
- __publicField$1(this, "params");
13
- this.rawPath = rawPath;
14
- this.params = params;
15
- }
16
- buildUrl() {
17
- return OpenAPIPath.setPathParams(this.rawPath, this.params);
18
- }
19
- static setPathParams(path, params) {
20
- const asEntries = Object.entries(params ?? {});
21
- const finalPath = asEntries.reduce((path2, entry) => {
22
- const [key, value] = entry;
23
- return path2.replace(`{${key}}`, encodeURIComponent(value));
24
- }, path);
25
- return finalPath.startsWith("/") ? finalPath.substring(1) : finalPath;
26
- }
27
- }
28
-
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 {
36
- constructor(operationDescriptor, requestObject) {
37
- __publicField(this, "operationDescriptor");
38
- __publicField(this, "requestObject");
39
- __publicField(this, "requestConfig");
40
- this.operationDescriptor = operationDescriptor;
41
- this.requestObject = requestObject;
42
- this.requestConfig = Object.freeze(this.buildAxiosConfig());
43
- }
44
- execute(axios) {
45
- return axios.request(this.requestConfig);
46
- }
47
- buildAxiosConfig() {
48
- const { method, path } = this.operationDescriptor;
49
- const pathParameters = this.requestObject;
50
- const openApiPath = new OpenAPIPath(path, pathParameters);
51
- const url = openApiPath.buildUrl();
52
- const data = this.requestObject && "data" in this.requestObject ? this.requestObject.data : void 0;
53
- const headersConfig = this.requestObject && "headers" in this.requestObject ? this.requestObject.headers : void 0;
54
- const headers = headersConfig ? this.makeAxiosHeaders(headersConfig) : void 0;
55
- const queryParametersConfig = this.requestObject && "queryParameters" in this.requestObject ? this.requestObject.queryParameters : void 0;
56
- const params = this.convertQueryToUrlSearchParams(queryParametersConfig);
57
- return {
58
- url,
59
- method,
60
- headers,
61
- // Must be a plain object or an URLSearchParams object
62
- params,
63
- data,
64
- validateStatus: () => true
65
- };
66
- }
67
- makeAxiosHeaders(headers) {
68
- return Object.fromEntries(
69
- Object.entries(headers).map(([key, value]) => [key, value?.toString()])
70
- );
71
- }
72
- convertQueryToUrlSearchParams(query) {
73
- if (query === void 0 || query === null) {
74
- return void 0;
75
- }
76
- if (query instanceof URLSearchParams) {
77
- return query;
78
- }
79
- if (typeof query === "string") {
80
- return new URLSearchParams(query);
81
- }
82
- if (typeof query === "object") {
83
- const searchParams = new URLSearchParams();
84
- for (const [key, value] of Object.entries(query)) {
85
- if (Array.isArray(value)) {
86
- for (const arrayItem of value) {
87
- searchParams.append(key, arrayItem);
88
- }
89
- } else {
90
- searchParams.append(
91
- key,
92
- typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? value.toString() : JSON.stringify(value)
93
- );
94
- }
95
- }
96
- return searchParams;
97
- }
98
- throw new Error(`Unexpected query parameter type (${typeof query})`);
99
- }
100
- }
101
-
102
- class ApiClientError extends AxiosError {
103
- constructor(message, code, config, request, response) {
104
- super(message, code, config, request, response);
105
- Object.setPrototypeOf(this, ApiClientError.prototype);
106
- this.name = "ApiClientError";
107
- }
108
- static fromResponse(message, response) {
109
- return new ApiClientError(
110
- message,
111
- void 0,
112
- response.config,
113
- response.request,
114
- response
115
- );
116
- }
117
- }
118
-
119
- function assertStatus(response, expectedStatus) {
120
- if (response.status !== expectedStatus) {
121
- throw ApiClientError.fromResponse(
122
- `Unexpected response status (expected ${expectedStatus}, got: ${response.status})`,
123
- response
124
- );
125
- }
126
- }
127
-
128
- export { ApiClientError as A, OpenAPIPath as O, Request as R, assertStatus as a };