Skip to content

Commit

Permalink
feat(ama-sdk): move ApiFetchClient to dedicated package
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Sep 2, 2024
1 parent afe641a commit fef26e3
Show file tree
Hide file tree
Showing 79 changed files with 2,794 additions and 9 deletions.
19 changes: 19 additions & 0 deletions packages/@ama-sdk/client-fetch/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable quote-props */

module.exports = {
'root': true,
'parserOptions': {
'tsconfigRootDir': __dirname,
'project': [
'tsconfig.build.json',
'tsconfig.builders.json',
'tsconfig.spec.json',
'tsconfig.eslint.json'
],
'sourceType': 'module'
},
'extends': [
'../../../.eslintrc.js'
]
};
10 changes: 10 additions & 0 deletions packages/@ama-sdk/client-fetch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/fwk
/helpers
/index.*
/bundles
/test
/dist-test
/plugins
/dist
/utils
/build
Empty file.
84 changes: 84 additions & 0 deletions packages/@ama-sdk/client-fetch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# @ama-sdk Core

[![Stable Version](https://img.shields.io/npm/v/@ama-sdk/core?style=for-the-badge)](https://www.npmjs.com/package/@ama-sdk/core)
[![Bundle Size](https://img.shields.io/bundlephobia/min/@ama-sdk/core?color=green&style=for-the-badge)](https://www.npmjs.com/package/@ama-sdk/core)

This package contains all the [plugins](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins), helpers and object definitions to dialog with an API following the `ama-sdk` architecture.

Please refer to the [ama-sdk-schematics](../schematics/README.md) package for getting started with an API based on `ama-sdk`.

## Available plugins

- [abort](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/abort)
- [additional-params](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/additional-params)
- [api-configuration-override](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/api-configuration-override)
- [api-key](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/api-key)
- [bot-protection-fingerprint](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/bot-protection-fingerprint)
- [concurrent](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/concurrent)
- [core](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/core)
- [client-facts](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/client-facts)
- [custom-info](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/custom-info)
- [exception](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/exception)
- [fetch-cache](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/fetch-cache)
- [fetch-credentials](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/fetch-credentials)
- [json-token](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/json-token)
- [keepalive](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/keepalive)
- [mock-intercept](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/mock-intercept)
- [perf-metric](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/perf-metric)
- [pii-tokenizer](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/pii-tokenizer)
- [raw-response-info](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/raw-response-info)
- [retry](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/retry)
- [reviver](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/reviver)
- [session-id](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/session-id)
- [si-token](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/si-token)
- [simple-api-key-authentication](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/simple-api-key-authentication)
- [url-rewrite](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/url-rewrite)
- [wait-for](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/wait-for)
- [timeout](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/plugins/timeout)

## Available API Client

The **API Clients** are mandatory to the SDK to indicate the service that should be used by the SDK to process the calls.
A list of API Clients are provided by this package:

| API Client | Import | Description |
|------------------|------------------------------------------|--------------------------------------------------------------------------------|
| ApiFetchClient | @ama-sdk/core | Default API Client based on the browser FetchApi |
| ApiBeaconClient | @ama-sdk/core | API Client based on the browser BeaconApi, it is processing synchronous call |
| ApiAngularClient | @ama-sdk/core/clients/api-angular-client | API Client using the HttpClient exposed by the `@angular/common` package |

### Logs

In order to ease the logging in the ama-sdk plugins, it is possible to connect to third-party logging services.
This can be achieved by adding a `Logger` [implementation](https://github.com/AmadeusITGroup/otter/tree/main/packages/%40ama-sdk/core/src/fwk/logger.ts) to the options of an API client.

For example, in the Otter showcase application, we could add a `ConsoleLogger` (from `@o3r/core`) as a parameter to the ApiFetchClient:

```typescript
const logger = new ConsoleLogger();
function petApiFactory() {
const apiConfig: ApiClient = new ApiFetchClient(
{
basePath: 'https://petstore3.swagger.io/api/v3',
requestPlugins: [new SessionIdRequest()],
fetchPlugins: [],
logger
}
);
return new PetApi(apiConfig);
}
```

> *Note*: Adding a third-party logging service is optional. If undefined, the fallback is the console logger.
### CLI

This package also comes with CLI scripts that can facilitate the upgrade and publication of an SDK.
Use --help on each command for more information

| Script | Description |
|-----------------------------|------------------------------------------------------------------------------------------------|
| amasdk-clear-index | Remove the index files that are no longer necessary after the deletion of the associated model |
| amasdk-files-pack | Prepare the dist folder for publication |
| amasdk-update-spec-from-npm | Update the OpenAPI spec from an NPM package |

11 changes: 11 additions & 0 deletions packages/@ama-sdk/client-fetch/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://raw.githubusercontent.com/angular/angular-cli/main/packages/angular_devkit/schematics/collection-schema.json",
"schematics": {
"ng-add": {
"description": "Add Otter ama-sdk-core to the project.",
"factory": "./schematics/ng-add/index#ngAdd",
"schema": "./schematics/ng-add/schema.json",
"aliases": ["install", "i"]
}
}
}
10 changes: 10 additions & 0 deletions packages/@ama-sdk/client-fetch/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const getJestGlobalConfig = require('../../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js',
'<rootDir>/testing/jest.config.ut.builders.js'
]
};
132 changes: 132 additions & 0 deletions packages/@ama-sdk/client-fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"name": "@ama-sdk/client-fetch",
"version": "0.0.0-placeholder",
"publishConfig": {
"access": "public"
},
"description": "API Request client for @ama-sdk/core based SDK",
"module": "dist/src/public_api.js",
"esm2015": "dist/esm2015/public_api.js",
"esm2020": "dist/src/public_api.js",
"typings": "dist/src/public_api.d.ts",
"sideEffects": false,
"exports": {
"./package.json": {
"default": "./package.json"
},
".": {
"module": "./dist/src/public_api.js",
"esm2020": "./dist/src/public_api.js",
"esm2015": "./dist/esm2015/public_api.js",
"es2020": "./dist/cjs/public_api.js",
"default": "./dist/cjs/public_api.js",
"typings": "./dist/src/public_api.d.ts",
"import": "./dist/src/public_api.js",
"node": "./dist/cjs/public_api.js",
"require": "./dist/cjs/public_api.js"
}
},
"scripts": {
"nx": "nx",
"ng": "yarn nx",
"build": "yarn nx build ama-sdk-core",
"build:cjs": "swc src -d dist/cjs -C module.type=commonjs -q --strip-leading-paths",
"build:esm2015": "swc src -d dist/esm2015 -C module.type=es6 -q --strip-leading-paths",
"build:esm2020": "tsc -b tsconfig.build.json",
"postbuild": "yarn cpy './package.json' dist && patch-package-json-main",
"prepare:build:builders": "yarn cpy 'schematics/**/*.json' dist/schematics && yarn cpy 'collection.json' dist",
"build:builders": "tsc -b tsconfig.builders.json --pretty && yarn generate-cjs-manifest",
"prepare:publish": "prepare-publish ./dist"
},
"dependencies": {
"@swc/helpers": "~0.5.0",
"tslib": "^2.6.2",
"uuid": "^10.0.0"
},
"peerDependencies": {
"@ama-sdk/core": "workspace:^",
"@angular-devkit/schematics": "~18.2.0",
"@angular/cli": "~18.2.0",
"@angular/common": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@schematics/angular": "~18.2.0",
"isomorphic-fetch": "^3.0.0",
"rxjs": "^7.8.1",
"typescript": "~5.5.4"
},
"peerDependenciesMeta": {
"@angular-devkit/schematics": {
"optional": true
},
"@angular/cli": {
"optional": true
},
"@angular/common": {
"optional": true
},
"@o3r/schematics": {
"optional": true
},
"@schematics/angular": {
"optional": true
},
"isomorphic-fetch": {
"optional": true
},
"rxjs": {
"optional": true
},
"typescript": {
"optional": true
}
},
"devDependencies": {
"@ama-sdk/core": "workspace:^",
"@angular-devkit/core": "~18.2.0",
"@angular-devkit/schematics": "~18.2.0",
"@angular-eslint/eslint-plugin": "~18.3.0",
"@angular/common": "~18.2.0",
"@angular/core": "~18.2.0",
"@nx/eslint-plugin": "~19.5.0",
"@nx/jest": "~19.5.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@schematics/angular": "~18.2.0",
"@stylistic/eslint-plugin-ts": "~2.4.0",
"@swc/cli": "~0.4.0",
"@swc/core": "~1.7.0",
"@types/jest": "~29.5.2",
"@types/node": "^20.0.0",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.14.1",
"@typescript-eslint/utils": "^7.14.1",
"copyfiles": "^2.4.1",
"cpy-cli": "^5.0.0",
"eslint": "^8.57.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-plugin-jest": "~28.8.0",
"eslint-plugin-jsdoc": "~48.11.0",
"eslint-plugin-prefer-arrow": "~1.2.3",
"eslint-plugin-unicorn": "^54.0.0",
"isomorphic-fetch": "~3.0.0",
"jest": "~29.7.0",
"jest-junit": "~16.0.0",
"jsonc-eslint-parser": "~2.4.0",
"minimist": "^1.2.6",
"pid-from-port": "^1.1.3",
"rimraf": "^5.0.1",
"rxjs": "^7.8.1",
"semver": "^7.5.2",
"ts-jest": "~29.2.0",
"ts-node": "~10.9.2",
"type-fest": "^4.10.2",
"typescript": "~5.5.4",
"zone.js": "~0.14.2"
},
"engines": {
"node": "^18.19.1 || ^20.11.1 || >=22.0.0"
},
"schematics": "./collection.json"
}
84 changes: 84 additions & 0 deletions packages/@ama-sdk/client-fetch/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"name": "ama-sdk-client-fetch",
"$schema": "https://raw.githubusercontent.com/nrwl/nx/master/packages/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "packages/@ama-sdk/core/src",
"prefix": "o3r",
"targets": {
"build": {
"executor": "nx:run-script",
"outputs": ["{projectRoot}/dist/package.json"],
"options": {
"script": "postbuild"
},
"dependsOn": [
"build-builders",
"compile",
"build-esm2015",
"build-cjs"
]
},
"build-esm2015": {
"executor": "nx:run-script",
"options": {
"script": "build:esm2015"
}
},
"build-cjs": {
"executor": "nx:run-script",
"options": {
"script": "build:cjs"
}
},
"compile": {
"executor": "nx:run-script",
"options": {
"script": "build:esm2020"
},
"outputs": ["{projectRoot}/dist/src"]
},
"lint": {
"options": {
"eslintConfig": "packages/@ama-sdk/core/.eslintrc.js",
"lintFilePatterns": [
"packages/@ama-sdk/core/src/**/*.ts",
"packages/@ama-sdk/core/package.json"
]
},
"dependsOn": [
"build"
]
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "packages/@ama-sdk/core/jest.config.js"
}
},
"prepare-publish": {
"executor": "nx:run-script",
"options": {
"script": "prepare:publish"
}
},
"publish": {
"executor": "nx:run-commands",
"options": {
"command": "npm publish packages/@ama-sdk/core/dist"
}
},
"prepare-build-builders": {
"executor": "nx:run-script",
"options": {
"script": "prepare:build:builders"
}
},
"build-builders": {
"executor": "nx:run-script",
"options": {
"script": "build:builders"
}
}
},
"tags": []
}
13 changes: 13 additions & 0 deletions packages/@ama-sdk/client-fetch/schematics/ng-add/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
This files is used to allow the usage of the builder within @o3r/framework mono-repository.
It should not be part of the package.
*/

const {resolve} = require('node:path');

require('ts-node').register({ project: resolve(__dirname, '..', '..', 'tsconfig.builders.json') });
require('ts-node').register = () => {};

module.exports = require('./index.ts');
20 changes: 20 additions & 0 deletions packages/@ama-sdk/client-fetch/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { callRule, Tree } from '@angular-devkit/schematics';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { firstValueFrom } from 'rxjs';
import { ngAdd } from './index';

describe('Ng add @ama-sdk/core', () => {
it('should update imports', async () => {
const initialTree = Tree.empty();
initialTree.create('angular.json', readFileSync(join(__dirname, 'mocks', 'angular.mocks.json')));
initialTree.create('src/example.ts', readFileSync(join(__dirname, 'mocks', 'example.ts.mock')));
const context: any = { addTask: jest.fn(), logger: { debug: jest.fn() }, schematic: { description: { name: 'schematic', collection: { name: '@scope/test' }}}};
const tree = await firstValueFrom(callRule(ngAdd({projectName: 'projectName'}), initialTree, context));
const newContent = tree.readText('src/example.ts');
expect(newContent).not.toContain('@dapi/sdk-core');
expect(newContent).toContain('from \'@ama-sdk/core\'');
expect(newContent).toContain('@whatever/package');
expect(context.addTask).toHaveBeenCalled();
});
});
Loading

0 comments on commit fef26e3

Please sign in to comment.