@ps-aux/api-client-axios 0.0.1

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.
@@ -0,0 +1,3 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpClient } from './mytyp';
3
+ export declare const createApiClient: (axios: AxiosInstance) => HttpClient;
@@ -0,0 +1,3 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpClient } from './mytyp';
3
+ export declare const createApiClient: (axios: AxiosInstance) => HttpClient;
@@ -0,0 +1 @@
1
+ export declare const convertToFormData: (payload: Record<string, any>) => FormData;
@@ -0,0 +1 @@
1
+ export { createApiClient } from './AxiosOpenApiHttpClient';
@@ -0,0 +1,21 @@
1
+ export type ContentType = {};
2
+ export type RequestParams = {
3
+ fileDownload?: boolean;
4
+ };
5
+ export declare const ContentType: {
6
+ Json: string;
7
+ FormData: string;
8
+ };
9
+ export type Request = {
10
+ path: string;
11
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
12
+ format?: 'json';
13
+ query?: any;
14
+ body?: any;
15
+ type?: string;
16
+ secure?: boolean;
17
+ fileDownload?: boolean;
18
+ };
19
+ export type HttpClient<Any = any> = {
20
+ request: <Data, A = any>(req: Request) => Promise<Data>;
21
+ };
@@ -0,0 +1 @@
1
+ export declare const serializeQuery: (obj: object) => string;
@@ -0,0 +1 @@
1
+ export declare const convertToFormData: (payload: Record<string, any>) => FormData;
@@ -0,0 +1 @@
1
+ export { createApiClient } from './AxiosOpenApiHttpClient';
@@ -0,0 +1,86 @@
1
+ import { partition, fromPairs } from 'rambda';
2
+ import { flatten as flatten$1 } from 'flat';
3
+
4
+ // Use this file also as a symlink for http-client.eta file
5
+ const ContentType = {
6
+ Json: 'application/json',
7
+ FormData: 'multipart/form-data',
8
+ };
9
+
10
+ const serializeQuery = (obj) => {
11
+ const parts = [];
12
+ Object.entries(obj).forEach(([key, val]) => {
13
+ if (Array.isArray(val)) {
14
+ val.forEach((v) => {
15
+ parts.push([key, v]);
16
+ });
17
+ }
18
+ else if (typeof val === 'object') {
19
+ objectToParams(val).forEach(([key, val]) => {
20
+ parts.push([key, val.toString()]);
21
+ });
22
+ }
23
+ else {
24
+ parts.push([key, val]);
25
+ }
26
+ });
27
+ return parts.map((p) => `${p[0]}=${p[1]}`).join('&');
28
+ };
29
+ const flatten = (obj) => {
30
+ const r = flatten$1(obj);
31
+ // { empty: {} } would be { empty: {} } instead of empty array
32
+ return Object.entries(r).filter(([k, v]) => typeof v !== 'object');
33
+ };
34
+ const objectToParams = (obj) => {
35
+ const [arrayProps, nonArrayProps] = partition((e) => Array.isArray(e[1]), Object.entries(obj));
36
+ const withoutArrayProps = fromPairs(nonArrayProps);
37
+ const res = flatten(withoutArrayProps);
38
+ arrayProps.forEach(([k, vals]) => {
39
+ vals.forEach((v) => res.push([k, v]));
40
+ });
41
+ return res;
42
+ };
43
+
44
+ const convertToFormData = (payload) => {
45
+ const formData = new FormData();
46
+ const addProp = (key, val) => {
47
+ if (Array.isArray(val) || val instanceof FileList) {
48
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
49
+ // @ts-ignore - seems that FileList is iterable despite the warning
50
+ // TODO change to other iteration method if this is not true
51
+ for (const valItem of val) {
52
+ addProp(key, valItem);
53
+ }
54
+ }
55
+ else if (typeof val === 'object' &&
56
+ val != null &&
57
+ !(val instanceof File)) {
58
+ throw new Error('Object serialization into FormData not supported');
59
+ }
60
+ else {
61
+ formData.append(key, val);
62
+ }
63
+ };
64
+ Object.entries(payload).forEach(([key, val]) => addProp(key, val));
65
+ return formData;
66
+ };
67
+
68
+ const createApiClient = (axios) => {
69
+ return {
70
+ request: (req) => {
71
+ const { query, type, body } = req;
72
+ const data = type === ContentType.FormData ? convertToFormData(body) : body;
73
+ return axios
74
+ .request({
75
+ method: req.method,
76
+ url: req.path + (query ? `?${serializeQuery(query)}` : ''),
77
+ // url: req.path,
78
+ data,
79
+ params: req.query,
80
+ })
81
+ .then((r) => r.data);
82
+ },
83
+ };
84
+ };
85
+
86
+ export { createApiClient };
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rambda'), require('flat')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'rambda', 'flat'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MyLibrary = {}, global.rambda, global.flat));
5
+ })(this, (function (exports, rambda, flat) { 'use strict';
6
+
7
+ // Use this file also as a symlink for http-client.eta file
8
+ const ContentType = {
9
+ Json: 'application/json',
10
+ FormData: 'multipart/form-data',
11
+ };
12
+
13
+ const serializeQuery = (obj) => {
14
+ const parts = [];
15
+ Object.entries(obj).forEach(([key, val]) => {
16
+ if (Array.isArray(val)) {
17
+ val.forEach((v) => {
18
+ parts.push([key, v]);
19
+ });
20
+ }
21
+ else if (typeof val === 'object') {
22
+ objectToParams(val).forEach(([key, val]) => {
23
+ parts.push([key, val.toString()]);
24
+ });
25
+ }
26
+ else {
27
+ parts.push([key, val]);
28
+ }
29
+ });
30
+ return parts.map((p) => `${p[0]}=${p[1]}`).join('&');
31
+ };
32
+ const flatten = (obj) => {
33
+ const r = flat.flatten(obj);
34
+ // { empty: {} } would be { empty: {} } instead of empty array
35
+ return Object.entries(r).filter(([k, v]) => typeof v !== 'object');
36
+ };
37
+ const objectToParams = (obj) => {
38
+ const [arrayProps, nonArrayProps] = rambda.partition((e) => Array.isArray(e[1]), Object.entries(obj));
39
+ const withoutArrayProps = rambda.fromPairs(nonArrayProps);
40
+ const res = flatten(withoutArrayProps);
41
+ arrayProps.forEach(([k, vals]) => {
42
+ vals.forEach((v) => res.push([k, v]));
43
+ });
44
+ return res;
45
+ };
46
+
47
+ const convertToFormData = (payload) => {
48
+ const formData = new FormData();
49
+ const addProp = (key, val) => {
50
+ if (Array.isArray(val) || val instanceof FileList) {
51
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
+ // @ts-ignore - seems that FileList is iterable despite the warning
53
+ // TODO change to other iteration method if this is not true
54
+ for (const valItem of val) {
55
+ addProp(key, valItem);
56
+ }
57
+ }
58
+ else if (typeof val === 'object' &&
59
+ val != null &&
60
+ !(val instanceof File)) {
61
+ throw new Error('Object serialization into FormData not supported');
62
+ }
63
+ else {
64
+ formData.append(key, val);
65
+ }
66
+ };
67
+ Object.entries(payload).forEach(([key, val]) => addProp(key, val));
68
+ return formData;
69
+ };
70
+
71
+ const createApiClient = (axios) => {
72
+ return {
73
+ request: (req) => {
74
+ const { query, type, body } = req;
75
+ const data = type === ContentType.FormData ? convertToFormData(body) : body;
76
+ return axios
77
+ .request({
78
+ method: req.method,
79
+ url: req.path + (query ? `?${serializeQuery(query)}` : ''),
80
+ // url: req.path,
81
+ data,
82
+ params: req.query,
83
+ })
84
+ .then((r) => r.data);
85
+ },
86
+ };
87
+ };
88
+
89
+ exports.createApiClient = createApiClient;
90
+
91
+ }));
@@ -0,0 +1 @@
1
+ export declare const isEven: (x: number) => boolean;
@@ -0,0 +1,21 @@
1
+ export type ContentType = {};
2
+ export type RequestParams = {
3
+ fileDownload?: boolean;
4
+ };
5
+ export declare const ContentType: {
6
+ Json: string;
7
+ FormData: string;
8
+ };
9
+ export type Request = {
10
+ path: string;
11
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
12
+ format?: 'json';
13
+ query?: any;
14
+ body?: any;
15
+ type?: string;
16
+ secure?: boolean;
17
+ fileDownload?: boolean;
18
+ };
19
+ export type HttpClient<Any = any> = {
20
+ request: <Data, A = any>(req: Request) => Promise<Data>;
21
+ };
@@ -0,0 +1 @@
1
+ export declare const serializeQuery: (obj: object) => string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@ps-aux/api-client-axios",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "build": "rollup -c",
8
+ "tc": "tsc"
9
+ },
10
+ "author": "",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "@types/axios": "^0.14.0",
14
+ "axios": "^1.6.0",
15
+ "flat": "5.0.2"
16
+ },
17
+ "devDependencies": {
18
+ "@types/axios": "^0.14.0",
19
+ "rollup": "^4.1.4",
20
+ "rollup-plugin-typescript2": "^0.36.0"
21
+ }
22
+ }
@@ -0,0 +1,17 @@
1
+ import typescript from 'rollup-plugin-typescript2';
2
+
3
+ export default {
4
+ input: 'src/index.ts',
5
+ output: [
6
+ {
7
+ file: 'dist/index.js',
8
+ format: 'umd',
9
+ name: 'MyLibrary',
10
+ },
11
+ {
12
+ file: 'dist/index.esm.js',
13
+ format: 'es',
14
+ },
15
+ ],
16
+ plugins: [typescript()],
17
+ };
@@ -0,0 +1,27 @@
1
+ import {AxiosInstance} from 'axios'
2
+ import {ContentType, HttpClient} from './mytyp'
3
+ import {serializeQuery} from './serializeQuery'
4
+ import {convertToFormData} from './convertFormData'
5
+
6
+ export const createApiClient = (
7
+ axios: AxiosInstance,
8
+ ): HttpClient => {
9
+ return {
10
+ request: (req) => {
11
+ const {query, type, body} = req
12
+
13
+ const data =
14
+ type === ContentType.FormData ? convertToFormData(body) : body
15
+
16
+ return axios
17
+ .request({
18
+ method: req.method,
19
+ url: req.path + (query ? `?${serializeQuery(query)}` : ''),
20
+ // url: req.path,
21
+ data,
22
+ params: req.query,
23
+ })
24
+ .then((r) => r.data)
25
+ },
26
+ }
27
+ }
@@ -0,0 +1,26 @@
1
+
2
+
3
+ export const convertToFormData = (payload: Record<string, any>): FormData => {
4
+ const formData = new FormData()
5
+ const addProp = (key: string, val: any) => {
6
+ if (Array.isArray(val) || val instanceof FileList) {
7
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
8
+ // @ts-ignore - seems that FileList is iterable despite the warning
9
+ // TODO change to other iteration method if this is not true
10
+ for (const valItem of val) {
11
+ addProp(key, valItem)
12
+ }
13
+ } else if (
14
+ typeof val === 'object' &&
15
+ val != null &&
16
+ !(val instanceof File)
17
+ ) {
18
+ throw new Error('Object serialization into FormData not supported')
19
+ } else {
20
+ formData.append(key, val)
21
+ }
22
+ }
23
+
24
+ Object.entries(payload).forEach(([key, val]) => addProp(key, val))
25
+ return formData
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export {createApiClient} from './AxiosOpenApiHttpClient'
package/src/mytyp.ts ADDED
@@ -0,0 +1,27 @@
1
+ // Use this file also as a symlink for http-client.eta file
2
+
3
+ export type ContentType = {}
4
+
5
+ export type RequestParams = {
6
+ fileDownload?: boolean
7
+ }
8
+
9
+ export const ContentType = {
10
+ Json: 'application/json',
11
+ FormData: 'multipart/form-data',
12
+ }
13
+
14
+ export type Request = {
15
+ path: string
16
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE'
17
+ format?: 'json'
18
+ query?: any
19
+ body?: any
20
+ type?: string
21
+ secure?: boolean
22
+ fileDownload?: boolean
23
+ }
24
+
25
+ export type HttpClient<Any = any> = {
26
+ request: <Data, A = any>(req: Request) => Promise<Data>
27
+ }
@@ -0,0 +1,47 @@
1
+ import { fromPairs, partition } from 'rambda'
2
+ import { flatten as _flatten } from 'flat'
3
+
4
+ type Param = [string, any]
5
+
6
+ export const serializeQuery = (obj: object): string => {
7
+ const parts: string[][] = []
8
+
9
+ Object.entries(obj).forEach(([key, val]) => {
10
+ if (Array.isArray(val)) {
11
+ val.forEach((v) => {
12
+ parts.push([key, v])
13
+ })
14
+ } else if (typeof val === 'object') {
15
+ objectToParams(val).forEach(([key, val]) => {
16
+ parts.push([key, val.toString()])
17
+ })
18
+ } else {
19
+ parts.push([key, val])
20
+ }
21
+ })
22
+
23
+ return parts.map((p) => `${p[0]}=${p[1]}`).join('&')
24
+ }
25
+
26
+ const flatten = (obj: object): [string, string | number][] => {
27
+ const r: any = _flatten(obj)
28
+
29
+ // { empty: {} } would be { empty: {} } instead of empty array
30
+ return Object.entries(r).filter(([k, v]) => typeof v !== 'object') as any
31
+ }
32
+
33
+ const objectToParams = (obj: Record<string, any>): Param[] => {
34
+ const [arrayProps, nonArrayProps] = partition(
35
+ (e) => Array.isArray(e[1]),
36
+ Object.entries(obj),
37
+ )
38
+ const withoutArrayProps = fromPairs(nonArrayProps)
39
+
40
+ const res = flatten(withoutArrayProps)
41
+
42
+ arrayProps.forEach(([k, vals]) => {
43
+ vals.forEach((v: any) => res.push([k, v]))
44
+ })
45
+
46
+ return res
47
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ }