Skip to content

Commit

Permalink
feat(ama-sdk-schematics): remove dependencies in mock api generation …
Browse files Browse the repository at this point in the history
…(include breaking change)
  • Loading branch information
kpanot committed Oct 23, 2024
1 parent 8559e3d commit 6470f81
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -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}}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@
"peerDependenciesMeta": {
"isomorphic-fetch": {
"optional": true
},
"@ama-sdk/client-fetch": {
"optional": true
}
},
"devDependencies": {
Expand All @@ -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 %>",
Expand Down Expand Up @@ -120,7 +116,6 @@
"@o3r/schematics": "<%= sdkCoreRange %>"
},<% } %>
"peerDependencies": {
"@ama-sdk/client-fetch": "<%= sdkCoreRange %>",
"@ama-sdk/core": "~<%= sdkCoreVersion %>",
"isomorphic-fetch": "<%= versions['isomorphic-fetch'] %>"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/@ama-sdk/showcase-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -115,7 +111,6 @@
"typescript": "~5.5.4"
},
"peerDependencies": {
"@ama-sdk/client-fetch": "workspace:^",
"@ama-sdk/core": "workspace:^",
"isomorphic-fetch": "~3.0.0"
},
Expand Down
38 changes: 14 additions & 24 deletions packages/@ama-sdk/showcase-sdk/src/spec/api-mock.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
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;
storeApi: api.StoreApi;
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)
};
}
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6470f81

Please sign in to comment.