From 6470f81b0bde83a094032acc4c1171fca0f0c2ea Mon Sep 17 00:00:00 2001 From: Kilian Panot Date: Wed, 23 Oct 2024 19:34:52 +0900 Subject: [PATCH] feat(ama-sdk-schematics): remove dependencies in mock api generation (include breaking change) --- .../typescriptFetch/spec/api-mock.mustache | 37 ++++++------------ .../templates/base/package.json.template | 5 --- packages/@ama-sdk/showcase-sdk/package.json | 5 --- .../showcase-sdk/src/spec/api-mock.ts | 38 +++++++------------ yarn.lock | 4 -- 5 files changed, 26 insertions(+), 63 deletions(-) diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/spec/api-mock.mustache b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/spec/api-mock.mustache index 0438dc1cc3..d53413c525 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/spec/api-mock.mustache +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/openapi-codegen-typescript/src/main/resources/typescriptFetch/spec/api-mock.mustache @@ -1,11 +1,10 @@ {{#apiInfo}} -import { type ApiClient, isApiClient } from '@ama-sdk/core'; -import { ApiFetchClient, type BaseApiFetchClientConstructor } from '@ama-sdk/core'; +import type { ApiClient } from '@ama-sdk/core'; import * as api from '../api'; -const MOCK_SERVER_BASE_PATH = 'http://localhost:10010/v2'; -const MOCK_SERVER = new ApiFetchClient({basePath: MOCK_SERVER_BASE_PATH}); +/** Mock Server default base path */ +export const MOCK_SERVER_BASE_PATH = 'http://localhost:10010/v2'; export interface Api { {{#apis}} @@ -15,33 +14,21 @@ export interface Api { {{/apis}} } -export const myApi: Api = { -{{#noEmptyLines}}{{#trimComma}}{{#apis}} -{{#operations}} - {{#camelize}}{{classname}}{{/camelize}}: new api.{{classname}}(MOCK_SERVER), -{{/operations}} -{{/apis}}{{/trimComma}}{{/noEmptyLines}} -}; - - /** * Retrieve mocked SDK Apis - * - * @param config configuration of the Api Client + * @param apiClient Api Client instance + * @example Default Mocked API usage + * ```typescript + * import { getMockedApi, MOCK_SERVER_BASE_PATH } from '@my/sdk/spec'; + * import { ApiFetchClient } from '@ama-sdk/client-fetch'; + * const mocks = getMockedApi(new ApiFetchClient({ basePath: MOCK_SERVER_BASE_PATH })); + * ``` */ -export function getMockedApi(config?: string | BaseApiFetchClientConstructor | ApiClient): Api { - let apiConfigObj: ApiClient = MOCK_SERVER; - if (typeof config === 'string') { - apiConfigObj = new ApiFetchClient({basePath: config}); - } else if (isApiClient(config)) { - apiConfigObj = config; - } else if (config) { - apiConfigObj = new ApiFetchClient(config); - } +export function getMockedApi(apiClient: ApiClient): Api { return { {{#noEmptyLines}}{{#trimComma}}{{#apis}} {{#operations}} - {{#camelize}}{{classname}}{{/camelize}}: new api.{{classname}}(apiConfigObj), + {{#camelize}}{{classname}}{{/camelize}}: new api.{{classname}}(apiClient), {{/operations}} {{/apis}}{{/trimComma}}{{/noEmptyLines}} }; diff --git a/packages/@ama-sdk/schematics/schematics/typescript/shell/templates/base/package.json.template b/packages/@ama-sdk/schematics/schematics/typescript/shell/templates/base/package.json.template index 6f236ba4c2..3fcae892ab 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/shell/templates/base/package.json.template +++ b/packages/@ama-sdk/schematics/schematics/typescript/shell/templates/base/package.json.template @@ -62,9 +62,6 @@ "peerDependenciesMeta": { "isomorphic-fetch": { "optional": true - }, - "@ama-sdk/client-fetch": { - "optional": true } }, "devDependencies": { @@ -80,7 +77,6 @@ "@schematics/angular": "<%= angularVersion %>", "@commitlint/config-conventional": "<%= versions['@commitlint/config-conventional'] %>", "@ama-sdk/schematics": "<%= sdkCoreRange %>", - "@ama-sdk/client-fetch": "<%= sdkCoreRange %>", "@ama-sdk/core": "<%= sdkCoreRange %>", "@o3r/eslint-config-otter": "<%= sdkCoreRange %>", "@o3r/eslint-plugin": "<%= sdkCoreRange %>", @@ -120,7 +116,6 @@ "@o3r/schematics": "<%= sdkCoreRange %>" },<% } %> "peerDependencies": { - "@ama-sdk/client-fetch": "<%= sdkCoreRange %>", "@ama-sdk/core": "~<%= sdkCoreVersion %>", "isomorphic-fetch": "<%= versions['isomorphic-fetch'] %>" }, diff --git a/packages/@ama-sdk/showcase-sdk/package.json b/packages/@ama-sdk/showcase-sdk/package.json index bc4de4df03..ce2ea07a41 100644 --- a/packages/@ama-sdk/showcase-sdk/package.json +++ b/packages/@ama-sdk/showcase-sdk/package.json @@ -59,15 +59,11 @@ "tslib": "^2.6.2" }, "peerDependenciesMeta": { - "@ama-sdk/client-fetch": { - "optional": true - }, "isomorphic-fetch": { "optional": true } }, "devDependencies": { - "@ama-sdk/client-fetch": "workspace:^", "@ama-sdk/core": "workspace:^", "@ama-sdk/schematics": "workspace:^", "@angular-devkit/core": "~18.2.0", @@ -115,7 +111,6 @@ "typescript": "~5.5.4" }, "peerDependencies": { - "@ama-sdk/client-fetch": "workspace:^", "@ama-sdk/core": "workspace:^", "isomorphic-fetch": "~3.0.0" }, diff --git a/packages/@ama-sdk/showcase-sdk/src/spec/api-mock.ts b/packages/@ama-sdk/showcase-sdk/src/spec/api-mock.ts index fbc29e4368..f1a3727adc 100644 --- a/packages/@ama-sdk/showcase-sdk/src/spec/api-mock.ts +++ b/packages/@ama-sdk/showcase-sdk/src/spec/api-mock.ts @@ -1,10 +1,9 @@ -import { ApiClient, isApiClient } from '@ama-sdk/core'; -import { ApiFetchClient, BaseApiFetchClientConstructor } from '@ama-sdk/client-fetch'; +import type { ApiClient } from '@ama-sdk/core'; import * as api from '../api'; -const MOCK_SERVER_BASE_PATH = 'http://localhost:10010/v2'; -const MOCK_SERVER = new ApiFetchClient({basePath: MOCK_SERVER_BASE_PATH}); +/** Mock Server default base path */ +export const MOCK_SERVER_BASE_PATH = 'http://localhost:10010/v2'; export interface Api { petApi: api.PetApi; @@ -12,29 +11,20 @@ export interface Api { userApi: api.UserApi; } -export const myApi: Api = { - petApi: new api.PetApi(MOCK_SERVER), - storeApi: new api.StoreApi(MOCK_SERVER), - userApi: new api.UserApi(MOCK_SERVER) -}; - - /** * Retrieve mocked SDK Apis - * @param config configuration of the Api Client + * @param apiClient Api Client instance + * @example Default Mocked API usage + * ```typescript + * import { getMockedApi, MOCK_SERVER_BASE_PATH } from '@ama-sdk/showcase-sdk/spec'; + * import { ApiFetchClient } from '@ama-sdk/client-fetch'; + * const mocks = getMockedApi(new ApiFetchClient({ basePath: MOCK_SERVER_BASE_PATH })); + * ``` */ -export function getMockedApi(config?: string | BaseApiFetchClientConstructor | ApiClient): Api { - let apiConfigObj: ApiClient = MOCK_SERVER; - if (typeof config === 'string') { - apiConfigObj = new ApiFetchClient({basePath: config}); - } else if (isApiClient(config)) { - apiConfigObj = config; - } else if (config) { - apiConfigObj = new ApiFetchClient(config); - } +export function getMockedApi(apiClient: ApiClient): Api { return { - petApi: new api.PetApi(apiConfigObj), - storeApi: new api.StoreApi(apiConfigObj), - userApi: new api.UserApi(apiConfigObj) + petApi: new api.PetApi(apiClient), + storeApi: new api.StoreApi(apiClient), + userApi: new api.UserApi(apiClient) }; } diff --git a/yarn.lock b/yarn.lock index 6fdd1654ba..a936b9de16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -719,7 +719,6 @@ __metadata: version: 0.0.0-use.local resolution: "@ama-sdk/showcase-sdk@workspace:packages/@ama-sdk/showcase-sdk" dependencies: - "@ama-sdk/client-fetch": "workspace:^" "@ama-sdk/core": "workspace:^" "@ama-sdk/schematics": "workspace:^" "@angular-devkit/core": "npm:~18.2.0" @@ -768,12 +767,9 @@ __metadata: typedoc: "npm:~0.26.0" typescript: "npm:~5.5.4" peerDependencies: - "@ama-sdk/client-fetch": "workspace:^" "@ama-sdk/core": "workspace:^" isomorphic-fetch: ~3.0.0 peerDependenciesMeta: - "@ama-sdk/client-fetch": - optional: true isomorphic-fetch: optional: true languageName: unknown