@mittwald/api-client-commons 4.1.0-alpha.1 → 4.2.0-alpha.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.
Files changed (43) hide show
  1. package/dist/cjs/core/ApiClientBase.js +1 -1
  2. package/dist/cjs/core/Request.d.ts +2 -3
  3. package/dist/cjs/core/Request.js +4 -5
  4. package/dist/cjs/core/Request.test.js +2 -2
  5. package/dist/cjs/react/ApiCallAsyncResourceFactory.d.ts +12 -0
  6. package/dist/cjs/react/ApiCallAsyncResourceFactory.js +52 -0
  7. package/dist/cjs/react/{getAsyncResourceFactory.test-types.js → ApiCallAsyncResourceFactory.test-types.js} +12 -14
  8. package/dist/cjs/react/{getAsyncResourceFactory.test.js → ApiCallAsyncResourceFactory.test.js} +10 -14
  9. package/dist/cjs/react/index.d.ts +1 -1
  10. package/dist/cjs/react/index.js +1 -1
  11. package/dist/cjs/react/types.d.ts +3 -0
  12. package/dist/cjs/react/types.js +2 -0
  13. package/dist/cjs/types/OpenAPIOperation.d.ts +7 -1
  14. package/dist/cjs/types/assertOneOfStatus.d.ts +4 -0
  15. package/dist/cjs/types/assertOneOfStatus.js +13 -0
  16. package/dist/cjs/types/index.d.ts +1 -1
  17. package/dist/cjs/types/index.js +1 -1
  18. package/dist/esm/core/ApiClientBase.js +1 -1
  19. package/dist/esm/core/Request.d.ts +2 -3
  20. package/dist/esm/core/Request.js +4 -6
  21. package/dist/esm/core/Request.test.js +2 -2
  22. package/dist/esm/react/ApiCallAsyncResourceFactory.d.ts +12 -0
  23. package/dist/esm/react/ApiCallAsyncResourceFactory.js +35 -0
  24. package/dist/esm/react/{getAsyncResourceFactory.test-types.js → ApiCallAsyncResourceFactory.test-types.js} +12 -14
  25. package/dist/esm/react/{getAsyncResourceFactory.test.js → ApiCallAsyncResourceFactory.test.js} +10 -14
  26. package/dist/esm/react/index.d.ts +1 -1
  27. package/dist/esm/react/index.js +1 -1
  28. package/dist/esm/react/types.d.ts +3 -0
  29. package/dist/esm/react/types.js +1 -0
  30. package/dist/esm/types/OpenAPIOperation.d.ts +7 -1
  31. package/dist/esm/types/assertOneOfStatus.d.ts +4 -0
  32. package/dist/esm/types/assertOneOfStatus.js +6 -0
  33. package/dist/esm/types/index.d.ts +1 -1
  34. package/dist/esm/types/index.js +1 -1
  35. package/package.json +2 -2
  36. package/dist/cjs/react/getAsyncResourceFactory.d.ts +0 -10
  37. package/dist/cjs/react/getAsyncResourceFactory.js +0 -23
  38. package/dist/esm/react/getAsyncResourceFactory.d.ts +0 -10
  39. package/dist/esm/react/getAsyncResourceFactory.js +0 -16
  40. /package/dist/cjs/react/{getAsyncResourceFactory.test-types.d.ts → ApiCallAsyncResourceFactory.test-types.d.ts} +0 -0
  41. /package/dist/cjs/react/{getAsyncResourceFactory.test.d.ts → ApiCallAsyncResourceFactory.test.d.ts} +0 -0
  42. /package/dist/esm/react/{getAsyncResourceFactory.test-types.d.ts → ApiCallAsyncResourceFactory.test-types.d.ts} +0 -0
  43. /package/dist/esm/react/{getAsyncResourceFactory.test.d.ts → ApiCallAsyncResourceFactory.test.d.ts} +0 -0
@@ -35,7 +35,7 @@ class ApiClientBase {
35
35
  axiosConfig instanceof axios_1.Axios ? axiosConfig : axios_1.default.create(axiosConfig);
36
36
  }
37
37
  requestFunctionFactory(operation) {
38
- return (conf) => new Request_js_1.default(this.axios, operation, conf).execute();
38
+ return (conf) => new Request_js_1.default(operation, conf).execute(this.axios);
39
39
  }
40
40
  }
41
41
  exports.ApiClientBase = ApiClientBase;
@@ -1,12 +1,11 @@
1
1
  import { OpenAPIOperation, RequestObject, ResponsePromise } from "../types/index.js";
2
2
  import { AxiosInstance, AxiosRequestConfig } from "axios";
3
3
  export declare class Request<TOp extends OpenAPIOperation> {
4
- private readonly axios;
5
4
  private readonly operationDescriptor;
6
5
  private readonly requestObject?;
7
6
  readonly requestConfig: AxiosRequestConfig;
8
- constructor(axiosInstance: AxiosInstance, operationDescriptor: TOp, request?: RequestObject<TOp>);
9
- execute(): ResponsePromise<TOp>;
7
+ constructor(operationDescriptor: TOp, requestObject?: RequestObject<TOp>);
8
+ execute(axios: AxiosInstance): ResponsePromise<TOp>;
10
9
  private buildAxiosConfig;
11
10
  private makeAxiosHeaders;
12
11
  private convertQueryToUrlSearchParams;
@@ -6,14 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Request = void 0;
7
7
  const OpenAPIPath_js_1 = __importDefault(require("./OpenAPIPath.js"));
8
8
  class Request {
9
- constructor(axiosInstance, operationDescriptor, request) {
10
- this.axios = axiosInstance;
9
+ constructor(operationDescriptor, requestObject) {
11
10
  this.operationDescriptor = operationDescriptor;
12
- this.requestObject = request;
11
+ this.requestObject = requestObject;
13
12
  this.requestConfig = Object.freeze(this.buildAxiosConfig());
14
13
  }
15
- execute() {
16
- return this.axios.request(this.requestConfig);
14
+ execute(axios) {
15
+ return axios.request(this.requestConfig);
17
16
  }
18
17
  buildAxiosConfig() {
19
18
  const { method, path } = this.operationDescriptor;
@@ -19,8 +19,8 @@ describe("query parameters", () => {
19
19
  method: "GET",
20
20
  };
21
21
  const executeRequest = (query) => {
22
- const request = new Request_js_1.default(mockedAxios, op, { queryParameters: query });
23
- request.execute();
22
+ const request = new Request_js_1.default(op, { queryParameters: query });
23
+ request.execute(mockedAxios);
24
24
  const requestConfig = requestFn.mock.calls[0][0];
25
25
  return requestConfig.params.toString();
26
26
  };
@@ -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,52 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ApiCallAsyncResourceFactory = void 0;
16
+ const react_use_promise_1 = require("@mittwald/react-use-promise");
17
+ const index_js_1 = require("../types/index.js");
18
+ const Request_js_1 = __importDefault(require("../core/Request.js"));
19
+ class ApiCallAsyncResourceFactory {
20
+ constructor(operation, requestFn) {
21
+ this.getApiResource = ((requestObj) => {
22
+ const request = new Request_js_1.default(this.operation, requestObj);
23
+ return (0, react_use_promise_1.getAsyncResource)((requestObj) => this.executeRequest(requestObj), [requestObj], {
24
+ tags: this.getAsyncResourceTags(request),
25
+ loaderId: this.getAsyncResourceId(),
26
+ });
27
+ });
28
+ this.operation = operation;
29
+ this.requestFn = requestFn;
30
+ }
31
+ getAsyncResourceId() {
32
+ return `${ApiCallAsyncResourceFactory.namespace}/${this.operation.operationId}`;
33
+ }
34
+ getAsyncResourceTags(request) {
35
+ var _a;
36
+ const url = (_a = request.requestConfig.url) !== null && _a !== void 0 ? _a : "";
37
+ return [
38
+ this.getAsyncResourceId(),
39
+ `${ApiCallAsyncResourceFactory.namespace}/${this.operation.method}`,
40
+ `${ApiCallAsyncResourceFactory.namespace}/${url}`,
41
+ ];
42
+ }
43
+ executeRequest(requestObj) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const response = yield this.requestFn(requestObj);
46
+ (0, index_js_1.assertStatus)(response, 200);
47
+ return response.data;
48
+ });
49
+ }
50
+ }
51
+ exports.ApiCallAsyncResourceFactory = ApiCallAsyncResourceFactory;
52
+ ApiCallAsyncResourceFactory.namespace = "@mittwald/api-client";
@@ -1,40 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const getAsyncResourceFactory_js_1 = require("./getAsyncResourceFactory.js");
4
- const getStuff = (0, getAsyncResourceFactory_js_1.getAsyncResourceFactory)({}, {
3
+ const ApiCallAsyncResourceFactory_js_1 = require("./ApiCallAsyncResourceFactory.js");
4
+ const getStuff = new ApiCallAsyncResourceFactory_js_1.ApiCallAsyncResourceFactory({
5
5
  operationId: "getStuff",
6
6
  path: "/stuff",
7
7
  method: "GET",
8
- });
8
+ }, {});
9
9
  function ignoredCheckRequestType() {
10
- getStuff({
10
+ getStuff.getApiResource({
11
11
  data: {
12
12
  // @ts-expect-error Not matching request type
13
13
  foo: "",
14
14
  },
15
15
  });
16
- getStuff({
16
+ getStuff.getApiResource({
17
17
  data: {
18
18
  requestString: "",
19
19
  },
20
20
  });
21
21
  }
22
22
  function ignoredCheckResponseType() {
23
- const stuff = getStuff({
23
+ const stuff = getStuff
24
+ .getApiResource({
24
25
  data: {
25
26
  requestString: "",
26
27
  },
27
- }).watch();
28
- // @ts-expect-error error
29
- if (stuff.status === 42) {
30
- // must be 200
31
- }
28
+ })
29
+ .watch();
32
30
  // @ts-expect-error Accessing unknown prop
33
- stuff.data.foo;
31
+ stuff.foo;
34
32
  (function (ignored) {
35
33
  // @ts-expect-error is a number
36
- })(stuff.data.responseNumber);
34
+ })(stuff.responseData);
37
35
  (function (ignored) {
38
36
  // is number
39
- })(stuff.data.responseNumber);
37
+ })(stuff.responseData);
40
38
  }
@@ -10,19 +10,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const globals_1 = require("@jest/globals");
13
- const getAsyncResourceFactory_js_1 = require("./getAsyncResourceFactory.js");
13
+ const ApiCallAsyncResourceFactory_js_1 = require("./ApiCallAsyncResourceFactory.js");
14
14
  const react_use_promise_1 = require("@mittwald/react-use-promise");
15
15
  (0, globals_1.beforeEach)(() => {
16
16
  (0, react_use_promise_1.refresh)();
17
17
  globals_1.jest.resetAllMocks();
18
18
  });
19
19
  const requestMock = globals_1.jest.fn();
20
- const axios = { request: requestMock };
21
- const getStuff = (0, getAsyncResourceFactory_js_1.getAsyncResourceFactory)(axios, {
20
+ const getStuff = new ApiCallAsyncResourceFactory_js_1.ApiCallAsyncResourceFactory({
22
21
  operationId: "getStuff",
23
22
  path: "/stuff",
24
23
  method: "GET",
25
- });
24
+ }, requestMock);
26
25
  const testRequest1 = {
27
26
  data: {
28
27
  foo: "bar",
@@ -34,28 +33,25 @@ const testRequest2 = {
34
33
  },
35
34
  };
36
35
  test("Resource loader executes request", () => __awaiter(void 0, void 0, void 0, function* () {
37
- yield getStuff(testRequest1).load();
36
+ yield getStuff.getApiResource(testRequest1).load();
38
37
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(1);
39
38
  const firstRequestParams = requestMock.mock.calls[0][0];
40
- (0, globals_1.expect)(firstRequestParams).toMatchObject({
41
- method: "GET",
42
- url: "stuff",
43
- });
39
+ (0, globals_1.expect)(firstRequestParams).toMatchObject(testRequest1);
44
40
  }));
45
41
  test("Resource is cached under URL", () => __awaiter(void 0, void 0, void 0, function* () {
46
- yield getStuff(testRequest1).load();
42
+ yield getStuff.getApiResource(testRequest1).load();
47
43
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(1);
48
- yield getStuff(testRequest1).load();
44
+ yield getStuff.getApiResource(testRequest1).load();
49
45
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(1);
50
46
  (0, react_use_promise_1.refresh)({
51
47
  tag: "@mittwald/api-client/stuff",
52
48
  });
53
- yield getStuff(testRequest1).load();
49
+ yield getStuff.getApiResource(testRequest1).load();
54
50
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(2);
55
51
  }));
56
52
  test("Resources are different when request object changes", () => __awaiter(void 0, void 0, void 0, function* () {
57
- yield getStuff(testRequest1).load();
53
+ yield getStuff.getApiResource(testRequest1).load();
58
54
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(1);
59
- yield getStuff(testRequest2).load();
55
+ yield getStuff.getApiResource(testRequest2).load();
60
56
  (0, globals_1.expect)(requestMock).toHaveBeenCalledTimes(2);
61
57
  }));
@@ -1 +1 @@
1
- export * from "./getAsyncResourceFactory.js";
1
+ export * from "./ApiCallAsyncResourceFactory.js";
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./getAsyncResourceFactory.js"), exports);
17
+ __exportStar(require("./ApiCallAsyncResourceFactory.js"), exports);
@@ -0,0 +1,3 @@
1
+ import { OpenAPIOperation, RequestObject, ResponseData } 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<ResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<ResponseData<TOp>>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,6 +1,6 @@
1
1
  import { AnyResponse, Response } from "./Response.js";
2
2
  import { AnyRequest, RequestType } from "./RequestType.js";
3
- import { HttpMethod } from "./http.js";
3
+ import { HttpMethod, HttpStatus } from "./http.js";
4
4
  export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestType, IgnoredResponse extends AnyResponse = Response> {
5
5
  operationId: string;
6
6
  path: string;
@@ -8,3 +8,9 @@ export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestTy
8
8
  }
9
9
  export type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
10
10
  export type InferredResponseType<TOp> = TOp extends OpenAPIOperation<RequestType, infer TRes> ? TRes : never;
11
+ export type ResponseData<TOp, TStatus extends HttpStatus = 200> = Extract<InferredResponseType<TOp>, {
12
+ status: TStatus;
13
+ }>["data"];
14
+ export type RequestData<TOp> = TOp extends OpenAPIOperation ? InferredRequestType<TOp> extends {
15
+ data: infer TData;
16
+ } ? TData : never : never;
@@ -0,0 +1,4 @@
1
+ import { Response } from "./Response.js";
2
+ export declare function assertOneOfStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S[]): asserts response is T & {
3
+ status: S;
4
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.assertOneOfStatus = void 0;
7
+ const ApiClientError_js_1 = __importDefault(require("../core/ApiClientError.js"));
8
+ function assertOneOfStatus(response, expectedStatus) {
9
+ if (!expectedStatus.includes(response.status)) {
10
+ throw ApiClientError_js_1.default.fromResponse(`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`, response);
11
+ }
12
+ }
13
+ exports.assertOneOfStatus = assertOneOfStatus;
@@ -3,6 +3,6 @@ export * from "./RequestFunction.js";
3
3
  export * from "./Response.js";
4
4
  export * from "./OpenAPIOperation.js";
5
5
  export * from "./http.js";
6
- export * from "./assertStatus.js";
7
6
  export * from "./simplify.js";
8
7
  export * from "./assertStatus.js";
8
+ export * from "./assertOneOfStatus.js";
@@ -19,6 +19,6 @@ __exportStar(require("./RequestFunction.js"), exports);
19
19
  __exportStar(require("./Response.js"), exports);
20
20
  __exportStar(require("./OpenAPIOperation.js"), exports);
21
21
  __exportStar(require("./http.js"), exports);
22
- __exportStar(require("./assertStatus.js"), exports);
23
22
  __exportStar(require("./simplify.js"), exports);
24
23
  __exportStar(require("./assertStatus.js"), exports);
24
+ __exportStar(require("./assertOneOfStatus.js"), exports);
@@ -7,7 +7,7 @@ export class ApiClientBase {
7
7
  axiosConfig instanceof Axios ? axiosConfig : axios.create(axiosConfig);
8
8
  }
9
9
  requestFunctionFactory(operation) {
10
- return (conf) => new Request(this.axios, operation, conf).execute();
10
+ return (conf) => new Request(operation, conf).execute(this.axios);
11
11
  }
12
12
  }
13
13
  export default ApiClientBase;
@@ -1,12 +1,11 @@
1
1
  import { OpenAPIOperation, RequestObject, ResponsePromise } from "../types/index.js";
2
2
  import { AxiosInstance, AxiosRequestConfig } from "axios";
3
3
  export declare class Request<TOp extends OpenAPIOperation> {
4
- private readonly axios;
5
4
  private readonly operationDescriptor;
6
5
  private readonly requestObject?;
7
6
  readonly requestConfig: AxiosRequestConfig;
8
- constructor(axiosInstance: AxiosInstance, operationDescriptor: TOp, request?: RequestObject<TOp>);
9
- execute(): ResponsePromise<TOp>;
7
+ constructor(operationDescriptor: TOp, requestObject?: RequestObject<TOp>);
8
+ execute(axios: AxiosInstance): ResponsePromise<TOp>;
10
9
  private buildAxiosConfig;
11
10
  private makeAxiosHeaders;
12
11
  private convertQueryToUrlSearchParams;
@@ -1,17 +1,15 @@
1
1
  import OpenAPIPath from "./OpenAPIPath.js";
2
2
  export class Request {
3
- axios;
4
3
  operationDescriptor;
5
4
  requestObject;
6
5
  requestConfig;
7
- constructor(axiosInstance, operationDescriptor, request) {
8
- this.axios = axiosInstance;
6
+ constructor(operationDescriptor, requestObject) {
9
7
  this.operationDescriptor = operationDescriptor;
10
- this.requestObject = request;
8
+ this.requestObject = requestObject;
11
9
  this.requestConfig = Object.freeze(this.buildAxiosConfig());
12
10
  }
13
- execute() {
14
- return this.axios.request(this.requestConfig);
11
+ execute(axios) {
12
+ return axios.request(this.requestConfig);
15
13
  }
16
14
  buildAxiosConfig() {
17
15
  const { method, path } = this.operationDescriptor;
@@ -14,8 +14,8 @@ describe("query parameters", () => {
14
14
  method: "GET",
15
15
  };
16
16
  const executeRequest = (query) => {
17
- const request = new Request(mockedAxios, op, { queryParameters: query });
18
- request.execute();
17
+ const request = new Request(op, { queryParameters: query });
18
+ request.execute(mockedAxios);
19
19
  const requestConfig = requestFn.mock.calls[0][0];
20
20
  return requestConfig.params.toString();
21
21
  };
@@ -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,35 @@
1
+ import { getAsyncResource } from "@mittwald/react-use-promise";
2
+ import { assertStatus, } from "../types/index.js";
3
+ import Request from "../core/Request.js";
4
+ export class ApiCallAsyncResourceFactory {
5
+ static namespace = "@mittwald/api-client";
6
+ operation;
7
+ requestFn;
8
+ constructor(operation, requestFn) {
9
+ this.operation = operation;
10
+ this.requestFn = requestFn;
11
+ }
12
+ getAsyncResourceId() {
13
+ return `${ApiCallAsyncResourceFactory.namespace}/${this.operation.operationId}`;
14
+ }
15
+ getAsyncResourceTags(request) {
16
+ const url = request.requestConfig.url ?? "";
17
+ return [
18
+ this.getAsyncResourceId(),
19
+ `${ApiCallAsyncResourceFactory.namespace}/${this.operation.method}`,
20
+ `${ApiCallAsyncResourceFactory.namespace}/${url}`,
21
+ ];
22
+ }
23
+ async executeRequest(requestObj) {
24
+ const response = await this.requestFn(requestObj);
25
+ assertStatus(response, 200);
26
+ return response.data;
27
+ }
28
+ getApiResource = ((requestObj) => {
29
+ const request = new Request(this.operation, requestObj);
30
+ return getAsyncResource((requestObj) => this.executeRequest(requestObj), [requestObj], {
31
+ tags: this.getAsyncResourceTags(request),
32
+ loaderId: this.getAsyncResourceId(),
33
+ });
34
+ });
35
+ }
@@ -1,38 +1,36 @@
1
- import { getAsyncResourceFactory } from "./getAsyncResourceFactory.js";
2
- const getStuff = getAsyncResourceFactory({}, {
1
+ import { ApiCallAsyncResourceFactory } from "./ApiCallAsyncResourceFactory.js";
2
+ const getStuff = new ApiCallAsyncResourceFactory({
3
3
  operationId: "getStuff",
4
4
  path: "/stuff",
5
5
  method: "GET",
6
- });
6
+ }, {});
7
7
  function ignoredCheckRequestType() {
8
- getStuff({
8
+ getStuff.getApiResource({
9
9
  data: {
10
10
  // @ts-expect-error Not matching request type
11
11
  foo: "",
12
12
  },
13
13
  });
14
- getStuff({
14
+ getStuff.getApiResource({
15
15
  data: {
16
16
  requestString: "",
17
17
  },
18
18
  });
19
19
  }
20
20
  function ignoredCheckResponseType() {
21
- const stuff = getStuff({
21
+ const stuff = getStuff
22
+ .getApiResource({
22
23
  data: {
23
24
  requestString: "",
24
25
  },
25
- }).watch();
26
- // @ts-expect-error error
27
- if (stuff.status === 42) {
28
- // must be 200
29
- }
26
+ })
27
+ .watch();
30
28
  // @ts-expect-error Accessing unknown prop
31
- stuff.data.foo;
29
+ stuff.foo;
32
30
  (function (ignored) {
33
31
  // @ts-expect-error is a number
34
- })(stuff.data.responseNumber);
32
+ })(stuff.responseData);
35
33
  (function (ignored) {
36
34
  // is number
37
- })(stuff.data.responseNumber);
35
+ })(stuff.responseData);
38
36
  }
@@ -1,17 +1,16 @@
1
1
  import { beforeEach, expect, jest } from "@jest/globals";
2
- import { getAsyncResourceFactory } from "./getAsyncResourceFactory.js";
2
+ import { ApiCallAsyncResourceFactory } from "./ApiCallAsyncResourceFactory.js";
3
3
  import { refresh } from "@mittwald/react-use-promise";
4
4
  beforeEach(() => {
5
5
  refresh();
6
6
  jest.resetAllMocks();
7
7
  });
8
8
  const requestMock = jest.fn();
9
- const axios = { request: requestMock };
10
- const getStuff = getAsyncResourceFactory(axios, {
9
+ const getStuff = new ApiCallAsyncResourceFactory({
11
10
  operationId: "getStuff",
12
11
  path: "/stuff",
13
12
  method: "GET",
14
- });
13
+ }, requestMock);
15
14
  const testRequest1 = {
16
15
  data: {
17
16
  foo: "bar",
@@ -23,28 +22,25 @@ const testRequest2 = {
23
22
  },
24
23
  };
25
24
  test("Resource loader executes request", async () => {
26
- await getStuff(testRequest1).load();
25
+ await getStuff.getApiResource(testRequest1).load();
27
26
  expect(requestMock).toHaveBeenCalledTimes(1);
28
27
  const firstRequestParams = requestMock.mock.calls[0][0];
29
- expect(firstRequestParams).toMatchObject({
30
- method: "GET",
31
- url: "stuff",
32
- });
28
+ expect(firstRequestParams).toMatchObject(testRequest1);
33
29
  });
34
30
  test("Resource is cached under URL", async () => {
35
- await getStuff(testRequest1).load();
31
+ await getStuff.getApiResource(testRequest1).load();
36
32
  expect(requestMock).toHaveBeenCalledTimes(1);
37
- await getStuff(testRequest1).load();
33
+ await getStuff.getApiResource(testRequest1).load();
38
34
  expect(requestMock).toHaveBeenCalledTimes(1);
39
35
  refresh({
40
36
  tag: "@mittwald/api-client/stuff",
41
37
  });
42
- await getStuff(testRequest1).load();
38
+ await getStuff.getApiResource(testRequest1).load();
43
39
  expect(requestMock).toHaveBeenCalledTimes(2);
44
40
  });
45
41
  test("Resources are different when request object changes", async () => {
46
- await getStuff(testRequest1).load();
42
+ await getStuff.getApiResource(testRequest1).load();
47
43
  expect(requestMock).toHaveBeenCalledTimes(1);
48
- await getStuff(testRequest2).load();
44
+ await getStuff.getApiResource(testRequest2).load();
49
45
  expect(requestMock).toHaveBeenCalledTimes(2);
50
46
  });
@@ -1 +1 @@
1
- export * from "./getAsyncResourceFactory.js";
1
+ export * from "./ApiCallAsyncResourceFactory.js";
@@ -1 +1 @@
1
- export * from "./getAsyncResourceFactory.js";
1
+ export * from "./ApiCallAsyncResourceFactory.js";
@@ -0,0 +1,3 @@
1
+ import { OpenAPIOperation, RequestObject, ResponseData } 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<ResponseData<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<ResponseData<TOp>>;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { AnyResponse, Response } from "./Response.js";
2
2
  import { AnyRequest, RequestType } from "./RequestType.js";
3
- import { HttpMethod } from "./http.js";
3
+ import { HttpMethod, HttpStatus } from "./http.js";
4
4
  export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestType, IgnoredResponse extends AnyResponse = Response> {
5
5
  operationId: string;
6
6
  path: string;
@@ -8,3 +8,9 @@ export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = RequestTy
8
8
  }
9
9
  export type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
10
10
  export type InferredResponseType<TOp> = TOp extends OpenAPIOperation<RequestType, infer TRes> ? TRes : never;
11
+ export type ResponseData<TOp, TStatus extends HttpStatus = 200> = Extract<InferredResponseType<TOp>, {
12
+ status: TStatus;
13
+ }>["data"];
14
+ export type RequestData<TOp> = TOp extends OpenAPIOperation ? InferredRequestType<TOp> extends {
15
+ data: infer TData;
16
+ } ? TData : never : never;
@@ -0,0 +1,4 @@
1
+ import { Response } from "./Response.js";
2
+ export declare function assertOneOfStatus<T extends Response, S extends T["status"]>(response: T, expectedStatus: S[]): asserts response is T & {
3
+ status: S;
4
+ };
@@ -0,0 +1,6 @@
1
+ import ApiClientError from "../core/ApiClientError.js";
2
+ export function assertOneOfStatus(response, expectedStatus) {
3
+ if (!expectedStatus.includes(response.status)) {
4
+ throw ApiClientError.fromResponse(`Unexpected response status (expected ${expectedStatus}, got: ${response.status})`, response);
5
+ }
6
+ }
@@ -3,6 +3,6 @@ export * from "./RequestFunction.js";
3
3
  export * from "./Response.js";
4
4
  export * from "./OpenAPIOperation.js";
5
5
  export * from "./http.js";
6
- export * from "./assertStatus.js";
7
6
  export * from "./simplify.js";
8
7
  export * from "./assertStatus.js";
8
+ export * from "./assertOneOfStatus.js";
@@ -3,6 +3,6 @@ export * from "./RequestFunction.js";
3
3
  export * from "./Response.js";
4
4
  export * from "./OpenAPIOperation.js";
5
5
  export * from "./http.js";
6
- export * from "./assertStatus.js";
7
6
  export * from "./simplify.js";
8
7
  export * from "./assertStatus.js";
8
+ export * from "./assertOneOfStatus.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mittwald/api-client-commons",
3
- "version": "4.1.0-alpha.1",
3
+ "version": "4.2.0-alpha.1",
4
4
  "description": "Common types and utilities for mittwald API clients",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/mittwald/api-client-js.git",
@@ -64,7 +64,7 @@
64
64
  },
65
65
  "devDependencies": {
66
66
  "@jest/globals": "^29.6.0",
67
- "@mittwald/react-use-promise": "^1.3.2",
67
+ "@mittwald/react-use-promise": "^1.3.3",
68
68
  "@types/jest": "^29.5.2",
69
69
  "@yarnpkg/pnpify": "^4.0.0-rc.48",
70
70
  "jest": "^29.6.1",
@@ -1,10 +0,0 @@
1
- import { AsyncResource } from "@mittwald/react-use-promise";
2
- import { InferredResponseType, OpenAPIOperation, RequestObject } from "../types/index.js";
3
- import { AxiosInstance } from "axios";
4
- type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseType<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseType<TOp>>;
5
- export declare const getAsyncResourceFactory: <TOp extends OpenAPIOperation<{
6
- headers?: Partial<{
7
- [TKey: string]: (string | number | boolean) | (string | number | boolean)[];
8
- }> | undefined;
9
- }, import("../types/Response.js").Response>>(axios: AxiosInstance, op: TOp) => GetApiResourceFn<TOp>;
10
- export {};
@@ -1,23 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAsyncResourceFactory = void 0;
7
- const react_use_promise_1 = require("@mittwald/react-use-promise");
8
- const Request_js_1 = __importDefault(require("../core/Request.js"));
9
- const prefix = "@mittwald/api-client/";
10
- const getAsyncResourceFactory = (axios, op) => ((requestObj) => {
11
- const request = new Request_js_1.default(axios, op, requestObj);
12
- const url = request.requestConfig.url;
13
- const loaderId = prefix + op.operationId;
14
- const tags = [loaderId, prefix + op.method];
15
- if (url) {
16
- tags.push(prefix + url);
17
- }
18
- return (0, react_use_promise_1.getAsyncResource)((ignored) => request.execute(), [requestObj], {
19
- tags,
20
- loaderId,
21
- });
22
- });
23
- exports.getAsyncResourceFactory = getAsyncResourceFactory;
@@ -1,10 +0,0 @@
1
- import { AsyncResource } from "@mittwald/react-use-promise";
2
- import { InferredResponseType, OpenAPIOperation, RequestObject } from "../types/index.js";
3
- import { AxiosInstance } from "axios";
4
- type GetApiResourceFn<TOp extends OpenAPIOperation> = null extends RequestObject<TOp> ? (conf?: RequestObject<TOp>) => AsyncResource<InferredResponseType<TOp>> : (conf: RequestObject<TOp>) => AsyncResource<InferredResponseType<TOp>>;
5
- export declare const getAsyncResourceFactory: <TOp extends OpenAPIOperation<{
6
- headers?: Partial<{
7
- [TKey: string]: (string | number | boolean) | (string | number | boolean)[];
8
- }> | undefined;
9
- }, import("../types/Response.js").Response>>(axios: AxiosInstance, op: TOp) => GetApiResourceFn<TOp>;
10
- export {};
@@ -1,16 +0,0 @@
1
- import { getAsyncResource } from "@mittwald/react-use-promise";
2
- import Request from "../core/Request.js";
3
- const prefix = "@mittwald/api-client/";
4
- export const getAsyncResourceFactory = (axios, op) => ((requestObj) => {
5
- const request = new Request(axios, op, requestObj);
6
- const url = request.requestConfig.url;
7
- const loaderId = prefix + op.operationId;
8
- const tags = [loaderId, prefix + op.method];
9
- if (url) {
10
- tags.push(prefix + url);
11
- }
12
- return getAsyncResource((ignored) => request.execute(), [requestObj], {
13
- tags,
14
- loaderId,
15
- });
16
- });