@ps-aux/api-client-axios 0.0.9-rc-7 → 0.0.9-rc-9

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.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@ps-aux/api-client-axios",
3
- "version": "0.0.9-rc-7",
3
+ "version": "0.0.9-rc-9",
4
4
  "main": "dist/node.js",
5
5
  "module": "dist/node.esm.js",
6
6
  "browser": "dist/browser.esm.js",
7
- "types": "dist/api-client-axios/src/node.d.ts",
7
+ "types": "dist/node.d.ts",
8
8
  "scripts": {
9
9
  "build": "rollup -c",
10
10
  "pub": "npm run build && npm --access public publish",
@@ -1,7 +0,0 @@
1
- import { AxiosInstance } from 'axios';
2
- import { HttpClient } from './t';
3
- import { PlatformHelper } from './platform/types';
4
- import { UrlConverter } from '@ps-aux/api-client-common/src';
5
- export declare const noOpUrlConverter: () => UrlConverter;
6
- export declare const springBootUrlConverter: () => UrlConverter;
7
- export declare const createHttpClient: (axios: AxiosInstance, platform: PlatformHelper, urlConverter?: UrlConverter) => HttpClient;
@@ -1,4 +0,0 @@
1
- import { AxiosInstance } from 'axios';
2
- import { HttpClient } from './t';
3
- export declare const createHttpClient: (axios: AxiosInstance, urlConverter?: import("@ps-aux/api-client-common/src").UrlConverter) => HttpClient;
4
- export * from './commonExports';
@@ -1,3 +0,0 @@
1
- export { UrlConverter } from '@ps-aux/api-client-common';
2
- export { HttpClient, Request, RequestParams } from './t';
3
- export { springBootUrlConverter, noOpUrlConverter } from './AxiosOpenApiHttpClient';
@@ -1,2 +0,0 @@
1
- import { PlatformHelper } from "./platform/types";
2
- export declare const convertToFormData: (payload: Record<string, any>, platform: PlatformHelper) => FormData;
@@ -1,4 +0,0 @@
1
- import { AxiosInstance } from 'axios';
2
- import { HttpClient } from './t';
3
- export declare const createHttpClient: (axios: AxiosInstance, urlConverter?: import("@ps-aux/api-client-common/src").UrlConverter) => HttpClient;
4
- export * from './commonExports';
@@ -1,10 +0,0 @@
1
- import { PlatformHelper } from './types';
2
- export declare class BrowserPlatFormHelper implements PlatformHelper {
3
- isFile: (obj: any) => boolean;
4
- getFileAndName: (obj: any) => {
5
- file: File;
6
- name: string;
7
- };
8
- isFileList: (obj: any) => boolean;
9
- newFormData: () => FormData;
10
- }
@@ -1 +0,0 @@
1
- export { BrowserPlatFormHelper as Platform } from './browser';
@@ -1,10 +0,0 @@
1
- import { PlatformHelper } from './types';
2
- export declare class NodePlatFormHelper implements PlatformHelper {
3
- isFile: (obj: any) => boolean;
4
- getFileAndName: (obj: any) => {
5
- file: any;
6
- name: any;
7
- };
8
- isFileList: (obj: any) => boolean;
9
- newFormData: () => FormData;
10
- }
@@ -1,9 +0,0 @@
1
- export type PlatformHelper = {
2
- newFormData: () => FormData;
3
- isFile: (obj: any) => boolean;
4
- getFileAndName: (obj: any) => {
5
- file: any;
6
- name: string;
7
- };
8
- isFileList: (obj: any) => boolean;
9
- };
@@ -1,18 +0,0 @@
1
- export type ContentType = {};
2
- export type RequestParams = {};
3
- export declare const ContentType: {
4
- Json: string;
5
- FormData: string;
6
- };
7
- export type Request = {
8
- path: string;
9
- method: 'GET' | 'POST' | 'PUT' | 'DELETE';
10
- format?: 'json' | 'document';
11
- query?: any;
12
- body?: any;
13
- type?: string;
14
- secure?: boolean;
15
- };
16
- export type HttpClient<Any = any> = {
17
- request: <Data, A = any>(req: Request) => Promise<Data>;
18
- };
@@ -1,9 +0,0 @@
1
- export { serializeQueryForSpringBoot } from './serializeQueryForSpringBoot';
2
- export { serializeQueryWithQs } from './serializeQueryWithQs';
3
- export type UrlConverter = (req: {
4
- path: string;
5
- query?: Record<string, unknown>;
6
- }) => {
7
- url: string;
8
- params?: any;
9
- };
@@ -1,5 +0,0 @@
1
- /**
2
- * Brings the 1st level to the 0th level.
3
- * Flats the object by nesting with '.' path separator.
4
- */
5
- export declare const serializeQueryForSpringBoot: (obj: object) => string;
@@ -1 +0,0 @@
1
- export declare const serializeQueryWithQs: (obj: object) => string;
@@ -1,78 +0,0 @@
1
- import { AxiosInstance } from 'axios'
2
- import { convertToFormData } from './convertFormData'
3
- import { PlatformHelper } from './platform/types'
4
- import {
5
- serializeQueryForSpringBoot,
6
- UrlConverter,
7
- PromiseHttpClient,
8
- ContentType,
9
- getFileNameFromContentDispositionHeader,
10
- } from '@ps-aux/api-client-common'
11
-
12
-
13
- export const noOpUrlConverter: () => UrlConverter = () => ({ query, path }) => {
14
- return {
15
- url: path,
16
- params: query,
17
- }
18
- }
19
-
20
- export const springBootUrlConverter: () => UrlConverter = () => ({ query, path }) => {
21
-
22
- return {
23
- url: path + (query ? `?${serializeQueryForSpringBoot(query)}` : ''),
24
- params: undefined,
25
- }
26
- }
27
-
28
-
29
- export const createHttpClient = (
30
- axios: AxiosInstance,
31
- platform: PlatformHelper,
32
- urlConverter: UrlConverter = noOpUrlConverter(),
33
- ): PromiseHttpClient => {
34
-
35
- return {
36
- request: (req) => {
37
- const { query, type, body } = req
38
-
39
- const isBlobResponse = req.format === 'document'
40
-
41
- const data =
42
- type === ContentType.FormData ? convertToFormData(body, platform) : body
43
-
44
- const headers: Record<string, string> = {}
45
- if (type) {
46
- headers['Content-Type'] = type
47
- }
48
- return axios
49
- .request({
50
- method: req.method,
51
- // url: req.path + (query ? `?${serializeQueryForNestJs(query)}` : ''),
52
- // params: req.query, // TODO make this customizable as different behaviour might be needed with different APIs
53
- // url: req.path,
54
- ...urlConverter(req),
55
- data,
56
- headers,
57
- responseType: isBlobResponse ? platform.getFileAxiosResponseType() : 'json',
58
- })
59
- .then(r => {
60
- const { data } = r
61
- if (isBlobResponse) {
62
- const contDist = r.headers['content-disposition']
63
- const fileName = getFileNameFromContentDispositionHeader(contDist)
64
-
65
- const type = r.headers['content-type']
66
-
67
- return new File([data as Blob], fileName, {
68
- type,
69
- })
70
- }
71
-
72
- return data
73
- })
74
- },
75
- }
76
- }
77
-
78
-
package/src/browser.ts DELETED
@@ -1,11 +0,0 @@
1
- import { AxiosInstance } from 'axios'
2
- import { createHttpClient as create, noOpUrlConverter } from './AxiosOpenApiHttpClient'
3
- import { BrowserPlatFormHelper } from './platform/browser'
4
- import { PromiseHttpClient, UrlConverter } from './commonExports'
5
-
6
-
7
- export const createHttpClient = (axios: AxiosInstance, urlConverter: UrlConverter = noOpUrlConverter()): PromiseHttpClient => {
8
- return create(axios, new BrowserPlatFormHelper(), urlConverter)
9
- }
10
-
11
- export * from './commonExports'
@@ -1,2 +0,0 @@
1
- export { UrlConverter, PromiseHttpClient, ContentType } from '@ps-aux/api-client-common'
2
- export { springBootUrlConverter, noOpUrlConverter } from './AxiosOpenApiHttpClient'
@@ -1,33 +0,0 @@
1
- import {PlatformHelper} from "./platform/types";
2
-
3
-
4
- export const convertToFormData = (payload: Record<string, any>, platform: PlatformHelper): FormData => {
5
- const formData = platform.newFormData()
6
-
7
- const addProp = (key: string, val: any) => {
8
- if (Array.isArray(val) || platform.isFileList(val)) {
9
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10
- // @ts-ignore - seems that FileList is iterable despite the warning
11
- // TODO change to other iteration method if this is not true
12
- for (const valItem of val) {
13
- addProp(key, valItem)
14
- }
15
- } else if (
16
- typeof val === 'object' &&
17
- val != null &&
18
- !platform.isFile(val)
19
- ) {
20
- throw new Error(`Object serialization into FormData not supported for object: ${val}`)
21
- } else {
22
- if (platform.isFile(val)) {
23
- const {file, name} = platform.getFileAndName(val)
24
- formData.append(key, file, name)
25
- } else {
26
- formData.append(key, val)
27
- }
28
- }
29
- }
30
-
31
- Object.entries(payload).forEach(([key, val]) => addProp(key, val))
32
- return formData
33
- }
package/src/node.ts DELETED
@@ -1,10 +0,0 @@
1
- import { AxiosInstance } from 'axios'
2
- import { createHttpClient as create, noOpUrlConverter } from './AxiosOpenApiHttpClient'
3
- import { NodePlatFormHelper } from './platform/node'
4
- import { PromiseHttpClient, UrlConverter } from './commonExports'
5
-
6
- export const createHttpClient = (axios: AxiosInstance, urlConverter: UrlConverter = noOpUrlConverter()): PromiseHttpClient => {
7
- return create(axios, new NodePlatFormHelper(), urlConverter)
8
- }
9
-
10
- export * from './commonExports'
@@ -1,24 +0,0 @@
1
- import { PlatformHelper } from './types'
2
-
3
- export class BrowserPlatFormHelper implements PlatformHelper {
4
-
5
- isFile = (obj: any) =>
6
- obj instanceof File
7
-
8
- getFileAndName = (obj: any) => {
9
- if (!(obj instanceof File))
10
- throw new Error(`Obj ${obj} is not a file`)
11
- return {
12
- file: obj,
13
- name: obj.name,
14
- }
15
- }
16
-
17
-
18
- isFileList = (obj: any) =>
19
- obj instanceof FileList
20
- // @ts-ignore
21
- newFormData = () => new FormData()
22
-
23
- getFileAxiosResponseType = (): 'blob' => 'blob'
24
- }
@@ -1,37 +0,0 @@
1
- import { PlatformHelper } from './types'
2
- import NodeFormData from 'form-data'
3
- import stream from 'stream'
4
-
5
- export class NodePlatFormHelper implements PlatformHelper {
6
-
7
- isFile = (obj: any) => {
8
- if (typeof obj !== 'object')
9
- return false
10
-
11
- const { file, name } = obj
12
-
13
- return typeof name === 'string' &&
14
- (Buffer.isBuffer(file)
15
- || file instanceof stream.Readable)
16
- }
17
-
18
- getFileAndName = (obj: any) => {
19
- if (!this.isFile(obj))
20
- throw new Error(`Obj ${obj} is not a file`)
21
- return {
22
- file: obj.file,
23
- name: obj.name,
24
- }
25
- }
26
-
27
-
28
- isFileList = (obj: any) => {
29
- // No FileList in node?
30
- return false
31
- }
32
- // @ts-ignore
33
- newFormData = (): FormData => new NodeFormData()
34
-
35
- getFileAxiosResponseType = (): 'arraybuffer' => 'arraybuffer'
36
-
37
- }
@@ -1,11 +0,0 @@
1
- export type PlatformHelper = {
2
- newFormData: () => FormData
3
- isFile: (obj: any) => boolean,
4
- getFileAndName: (obj: any) => {
5
- file: any,
6
- name: string
7
- }
8
- isFileList: (obj: any) => boolean
9
-
10
- getFileAxiosResponseType: () => 'blob' | 'arraybuffer'
11
- }
package/test/my.spec.ts DELETED
@@ -1,22 +0,0 @@
1
- import { AxiosApi } from '@ps-aux/api-client-api-testing/apis/axios/client'
2
- import Axios from 'axios'
3
- import { createHttpClient } from '../src/AxiosOpenApiHttpClient'
4
- import { NodePlatFormHelper } from '../src/platform/node'
5
- import { createHttpServer, MirroredReqRes } from '../../helpers/http-server'
6
-
7
- const Api = (url: string) =>
8
- new AxiosApi(createHttpClient(Axios.create({ baseURL: url }), new NodePlatFormHelper()))
9
-
10
- it('test', async () => {
11
-
12
- const server = createHttpServer()
13
- const { url } = await server.start()
14
- const api = Api(url)
15
-
16
- try {
17
- await api.app.getHello({ name: 'world' })
18
- expect(server.lastRequest().url).toBe('/api/hello?name=world')
19
- } finally {
20
- await server.stop()
21
- }
22
- })