1.0.56 • Published 2 years ago

@paxada/axios-client-generator v1.0.56

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

axios-client-generator

Create an axios client package from a paxada project.

What you need before

  • You need a project generated by Paxada.
  • All your .doc.ts route file should be up-to-date.
  • All the .route.ts of the project should have a different name, even between different folders.
  • In your .interface.ts files in the routes folder do not use type that just returns void.

Initialization

npm i -D @paxada/axios-client-generator

Add in your package.json, in "scripts" field :

"generate:client": "paxada-axios-client-generate",
"publish:client": "cd {packageName} && npm run package-publish" 

Check your paxada project

  • Every route need a .interface.ts file.
  • Check well .doc.ts.
  • Check tha your TypeScript compiles by using npm run checkTs.

Generate the client

Run npm run generate:client to generate the client.

Configs

Add a axiosClient.config.json file at the root project.

{
  "folderName": "string",
  "packageName": "string",
  "extraExports": "[string]",
  "excludedRoutes": "[string]",
  "includedRoutes": "[string]" 
}

All fields are optional.

  • folderName: The folder name of the generated client. Default: ${projectName}-client.
  • packageName: The name in the package.json of the generated client. . Default: ${projectName}-client
  • extraExports: Paths from your utils files in your project to export in the generated client.
  • includedRoutes: Routes to include, the others will be excluded (from src/routes, ex: 'private/Companies').
  • excludedRoutes: Routes to exclude, the others will be included (from src/routes, ex: 'admin').

CLI options

paxada-axios-client-generator -h

-e, --extra-export <paths...> Add extra export paths
-fn, --folder-name <string> Package alias in package.json name
-ir, --included-route <routes...> Included routes from src/routes
-er, --excluded-routes <routes...> Excluded routes from src/routes
-pn, --package-name <string> Package name
-cf, --config-file <string> Config .json file to generate the route. Default: axiosClient.config.json

Exemple: \ paxada-axios-generate -fn bouncer-client -pn @waapi/bouncer-client -e src/clientUtils/index.ts\ \ CLI options will overwrite the axiosClient.config.json data.

Publish

npm run publish:client \ It will automatically patch the package version.

Warning

You should never update the generated client's code but the package version.\ It will be overwritten anyway at the next generation.\ The client catch the request errors so the methods will never throw. Instead, you should refer to the hasFailed and error fields of the promise response.

Initialize your client package on another project

Example:\ src/loaders/myProjectClient.ts

import { getAxiosClient, AxiosClient } from '@waapi/myProject-client';
import { MY_PROJECT_API_URL } from '@/config';

let myProjectClient: AxiosClient | undefined;

const createMyProjectClient = (url: string) => {
    return getAxiosClient({ baseUrl: url, headers: {} });
};

export const getMyProjectClient = (): AxiosClient => {
    if (myProjectClient === undefined) {
        if (MY_PROJECT_API_URL === undefined) throw new Error('Missing MY_PROJECT_API_URL');
        myProjectClient = createMyProjectClient(MY_PROJECT_API_URL);
    }
    return myProjectClient;
};

Response type

type Response<Data> = 
    | { hasFailed: true; error: { code: string; message: string } }
    | { hasFailed: false; data: Data }

Initialize your client mock method on another project

src/helpers/mockMyProjectClient

import { mockAxiosClient } from '@waapi/myProject-client';
import * as myProjectClient from '../loaders/myProjectClient';

export const mockMyProjectClient = () => {
    const mocked = mockAxiosClient<jest.Mock>(jest.fn);
    jest.spyOn(myProjectClient, 'getMyProjectClient').mockReturnValue(mocked);
    return mocked;
};

How to use the client mock

.test.ts file

import { mockMyProjectClient } from '@/helpers/mockMyProjectClient';
import { getMyProjectClient } from '@/loaders/myProjectClient';

const doSomething = async () => {
    const myProjectClient = getMyProjectClient()
    const response = await myProjectClient.setHeaders({ machin: "chouette" }).private.entity.getMethod();
    if (data.hasFailed) return "error";
    return response.data;
}

describe(() => {
    it ("Should call the mocked method", async () => {
        const myProjectClientMock = mockMyProjectClient();
        myProjectClientMock.private.entity.getMethod.mockResolvedValue({ hasFailed: false, data: "bidule" });
        
        const result = await doSomething();
        
        expect(myProjectClientMock.private.entity.getMethod).toHaveBeenCalled();
        expect(result).toBe("bidule");
    })
})
1.0.49

2 years ago

1.0.56

2 years ago

1.0.44

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.51

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.52

2 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.27

3 years ago

1.0.19

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago