@ps-aux/api-client-axios 0.0.2 → 0.0.4

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.
@@ -1,3 +1,3 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { HttpClient } from './mytyp';
3
- export declare const createApiClient: (axios: AxiosInstance) => HttpClient;
3
+ export declare const createApiClient: (axios: AxiosInstance, platformType: 'browser' | 'node') => HttpClient;
@@ -1 +1,2 @@
1
- export declare const convertToFormData: (payload: Record<string, any>) => FormData;
1
+ import { PlatformHelper } from "./platform/types";
2
+ export declare const convertToFormData: (payload: Record<string, any>, platform: PlatformHelper) => FormData;
package/dist/index.esm.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { partition, fromPairs } from 'rambda';
2
2
  import { flatten as flatten$1 } from 'flat';
3
+ import NodeFormData from 'form-data';
4
+ import stream from 'stream';
3
5
 
4
6
  // Use this file also as a symlink for http-client.eta file
5
7
  const ContentType = {
@@ -41,10 +43,10 @@ const objectToParams = (obj) => {
41
43
  return res;
42
44
  };
43
45
 
44
- const convertToFormData = (payload) => {
45
- const formData = new FormData();
46
+ const convertToFormData = (payload, platform) => {
47
+ const formData = platform.newFormData();
46
48
  const addProp = (key, val) => {
47
- if (Array.isArray(val) || val instanceof FileList) {
49
+ if (Array.isArray(val) || platform.isFileList(val)) {
48
50
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
49
51
  // @ts-ignore - seems that FileList is iterable despite the warning
50
52
  // TODO change to other iteration method if this is not true
@@ -54,22 +56,73 @@ const convertToFormData = (payload) => {
54
56
  }
55
57
  else if (typeof val === 'object' &&
56
58
  val != null &&
57
- !(val instanceof File)) {
58
- throw new Error('Object serialization into FormData not supported');
59
+ !platform.isFile(val)) {
60
+ throw new Error(`Object serialization into FormData not supported for object: ${val}`);
59
61
  }
60
62
  else {
61
- formData.append(key, val);
63
+ if (platform.isFile(val)) {
64
+ const { file, name } = platform.getFileAndName(val);
65
+ formData.append(key, file, name);
66
+ }
67
+ else {
68
+ formData.append(key, val);
69
+ }
62
70
  }
63
71
  };
64
72
  Object.entries(payload).forEach(([key, val]) => addProp(key, val));
65
73
  return formData;
66
74
  };
67
75
 
68
- const createApiClient = (axios) => {
76
+ class BrowserPlatFormHelper {
77
+ constructor() {
78
+ this.isFile = obj => obj instanceof File;
79
+ this.getFileAndName = (obj) => {
80
+ if (!(obj instanceof File))
81
+ throw new Error(`Obj ${obj} is not a file`);
82
+ return {
83
+ file: obj,
84
+ name: obj.name
85
+ };
86
+ };
87
+ this.isFileList = obj => obj instanceof FileList;
88
+ // @ts-ignore
89
+ this.newFormData = () => new FormData();
90
+ }
91
+ }
92
+
93
+ class NodePlatFormHelper {
94
+ constructor() {
95
+ this.isFile = obj => {
96
+ if (typeof obj !== 'object')
97
+ return false;
98
+ const { file, name } = obj;
99
+ return typeof name === 'string' &&
100
+ (Buffer.isBuffer(file)
101
+ || file instanceof stream.Readable);
102
+ };
103
+ this.getFileAndName = (obj) => {
104
+ if (!this.isFile(obj))
105
+ throw new Error(`Obj ${obj} is not a file`);
106
+ return {
107
+ file: obj.file,
108
+ name: obj.name,
109
+ };
110
+ };
111
+ this.isFileList = obj => {
112
+ // No FileList in node?
113
+ return false;
114
+ };
115
+ // @ts-ignore
116
+ this.newFormData = () => new NodeFormData();
117
+ }
118
+ }
119
+
120
+ const createApiClient = (axios, platformType) => {
121
+ const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper();
69
122
  return {
70
123
  request: (req) => {
71
124
  const { query, type, body } = req;
72
- const data = type === ContentType.FormData ? convertToFormData(body) : body;
125
+ const data = type === ContentType.FormData ? convertToFormData(body, platform) : body;
73
126
  return axios
74
127
  .request({
75
128
  method: req.method,
package/dist/index.js CHANGED
@@ -1,91 +1,139 @@
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';
1
+ (function(global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rambda'), require('flat'), require('form-data'), require('stream')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'rambda', 'flat', 'form-data', 'stream'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MyLibrary = {}, global.rambda, global.flat, global.NodeFormData, global.stream))
5
+ })(this, (function(exports, rambda, flat, NodeFormData, stream) {
6
+ 'use strict'
6
7
 
7
8
  // Use this file also as a symlink for http-client.eta file
8
9
  const ContentType = {
9
10
  Json: 'application/json',
10
11
  FormData: 'multipart/form-data',
11
- };
12
+ }
12
13
 
13
14
  const serializeQuery = (obj) => {
14
- const parts = [];
15
+ const parts = []
15
16
  Object.entries(obj).forEach(([key, val]) => {
16
17
  if (Array.isArray(val)) {
17
18
  val.forEach((v) => {
18
- parts.push([key, v]);
19
- });
20
- }
21
- else if (typeof val === 'object') {
19
+ parts.push([key, v])
20
+ })
21
+ } else if (typeof val === 'object') {
22
22
  objectToParams(val).forEach(([key, val]) => {
23
- parts.push([key, val.toString()]);
24
- });
25
- }
26
- else {
27
- parts.push([key, val]);
23
+ parts.push([key, val.toString()])
24
+ })
25
+ } else {
26
+ parts.push([key, val])
28
27
  }
29
- });
30
- return parts.map((p) => `${p[0]}=${p[1]}`).join('&');
31
- };
28
+ })
29
+ return parts.map((p) => `${p[0]}=${p[1]}`).join('&')
30
+ }
32
31
  const flatten = (obj) => {
33
- const r = flat.flatten(obj);
32
+ const r = flat.flatten(obj)
34
33
  // { empty: {} } would be { empty: {} } instead of empty array
35
- return Object.entries(r).filter(([k, v]) => typeof v !== 'object');
36
- };
34
+ return Object.entries(r).filter(([k, v]) => typeof v !== 'object')
35
+ }
37
36
  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);
37
+ const [arrayProps, nonArrayProps] = rambda.partition((e) => Array.isArray(e[1]), Object.entries(obj))
38
+ const withoutArrayProps = rambda.fromPairs(nonArrayProps)
39
+ const res = flatten(withoutArrayProps)
41
40
  arrayProps.forEach(([k, vals]) => {
42
- vals.forEach((v) => res.push([k, v]));
43
- });
44
- return res;
45
- };
41
+ vals.forEach((v) => res.push([k, v]))
42
+ })
43
+ return res
44
+ }
46
45
 
47
- const convertToFormData = (payload) => {
48
- const formData = new FormData();
46
+ const convertToFormData = (payload, platform) => {
47
+ const formData = platform.newFormData()
49
48
  const addProp = (key, val) => {
50
- if (Array.isArray(val) || val instanceof FileList) {
49
+ if (Array.isArray(val) || platform.isFileList(val)) {
51
50
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
51
  // @ts-ignore - seems that FileList is iterable despite the warning
53
52
  // TODO change to other iteration method if this is not true
54
53
  for (const valItem of val) {
55
- addProp(key, valItem);
54
+ addProp(key, valItem)
56
55
  }
57
- }
58
- else if (typeof val === 'object' &&
56
+ } else if (typeof val === 'object' &&
59
57
  val != null &&
60
- !(val instanceof File)) {
61
- throw new Error('Object serialization into FormData not supported');
58
+ !platform.isFile(val)) {
59
+ throw new Error(`Object serialization into FormData not supported for object: ${val}`)
60
+ } else {
61
+ if (platform.isFile(val)) {
62
+ const { file, name } = platform.getFileAndName(val)
63
+ formData.append(key, file, name)
64
+ } else {
65
+ formData.append(key, val)
66
+ }
62
67
  }
63
- else {
64
- formData.append(key, val);
68
+ }
69
+ Object.entries(payload).forEach(([key, val]) => addProp(key, val))
70
+ return formData
71
+ }
72
+
73
+ class BrowserPlatFormHelper {
74
+ constructor() {
75
+ this.isFile = obj => obj instanceof File
76
+ this.getFileAndName = (obj) => {
77
+ if (!(obj instanceof File))
78
+ throw new Error(`Obj ${obj} is not a file`)
79
+ return {
80
+ file: obj,
81
+ name: obj.name,
82
+ }
65
83
  }
66
- };
67
- Object.entries(payload).forEach(([key, val]) => addProp(key, val));
68
- return formData;
69
- };
84
+ this.isFileList = obj => obj instanceof FileList
85
+ // @ts-ignore
86
+ this.newFormData = () => new FormData()
87
+ }
88
+ }
70
89
 
71
- const createApiClient = (axios) => {
90
+ class NodePlatFormHelper {
91
+ constructor() {
92
+ this.isFile = obj => {
93
+ if (typeof obj !== 'object')
94
+ return false
95
+ const { file, name } = obj
96
+ return typeof name === 'string' &&
97
+ (Buffer.isBuffer(file)
98
+ || file instanceof stream.Readable)
99
+ }
100
+ this.getFileAndName = (obj) => {
101
+ if (!this.isFile(obj))
102
+ throw new Error(`Obj ${obj} is not a file`)
103
+ return {
104
+ file: obj.file,
105
+ name: obj.name,
106
+ }
107
+ }
108
+ this.isFileList = obj => {
109
+ // No FileList in node?
110
+ return false
111
+ }
112
+ // @ts-ignore
113
+ this.newFormData = () => new NodeFormData()
114
+ }
115
+ }
116
+
117
+ const createApiClient = (axios, platformType) => {
118
+ const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper()
72
119
  return {
73
120
  request: (req) => {
74
- const { query, type, body } = req;
75
- const data = type === ContentType.FormData ? convertToFormData(body) : body;
121
+ const { query, type, body } = req
122
+ const data = type === ContentType.FormData ? convertToFormData(body, platform) : body
123
+
76
124
  return axios
77
125
  .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);
126
+ method: req.method,
127
+ url: req.path + (query ? `?${serializeQuery(query)}` : ''),
128
+ // url: req.path,
129
+ data,
130
+ params: req.query
131
+ })
132
+ .then((r) => r.data)
85
133
  },
86
- };
87
- };
134
+ }
135
+ }
88
136
 
89
- exports.createApiClient = createApiClient;
137
+ exports.createApiClient = createApiClient
90
138
 
91
- }));
139
+ }))
@@ -0,0 +1,10 @@
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: () => any;
10
+ }
@@ -0,0 +1,2 @@
1
+ export { BrowserPlatFormHelper } from "./browser";
2
+ export { NodePlatFormHelper } from "./node";
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,9 @@
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
+ };
package/foo.sh ADDED
@@ -0,0 +1,3 @@
1
+ $#!bin/env bash
2
+
3
+ echo "fooooooooooo.sh"
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@ps-aux/api-client-axios",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
+ "bin": {
7
+ "aca": "./foo.sh"
8
+ },
6
9
  "scripts": {
7
10
  "build": "rollup -c",
8
11
  "tc": "tsc"
@@ -10,13 +13,12 @@
10
13
  "author": "",
11
14
  "license": "ISC",
12
15
  "dependencies": {
13
- "@types/axios": "^0.14.0",
14
- "axios": "^1.6.0",
16
+ "axios": "^1.5.0",
15
17
  "flat": "5.0.2",
18
+ "form-data": "^4.0.0",
16
19
  "rambda": "^8.5.0"
17
20
  },
18
21
  "devDependencies": {
19
- "@types/axios": "^0.14.0",
20
22
  "rollup": "^4.1.4",
21
23
  "rollup-plugin-typescript2": "^0.36.0"
22
24
  }
@@ -1,17 +1,22 @@
1
- import {AxiosInstance} from 'axios'
2
- import {ContentType, HttpClient} from './mytyp'
3
- import {serializeQuery} from './serializeQuery'
4
- import {convertToFormData} from './convertFormData'
1
+ import { AxiosInstance } from 'axios'
2
+ import { ContentType, HttpClient } from './mytyp'
3
+ import { serializeQuery } from './serializeQuery'
4
+ import { convertToFormData } from './convertFormData'
5
+ import { BrowserPlatFormHelper, NodePlatFormHelper } from './platform'
6
+ import { PlatformHelper } from './platform/types'
5
7
 
6
8
  export const createApiClient = (
7
9
  axios: AxiosInstance,
10
+ platformType: 'browser' | 'node',
8
11
  ): HttpClient => {
12
+ const platform: PlatformHelper = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper()
9
13
  return {
10
14
  request: (req) => {
11
- const {query, type, body} = req
15
+ const { query, type, body } = req
16
+
12
17
 
13
18
  const data =
14
- type === ContentType.FormData ? convertToFormData(body) : body
19
+ type === ContentType.FormData ? convertToFormData(body, platform) : body
15
20
 
16
21
  return axios
17
22
  .request({
@@ -20,6 +25,9 @@ export const createApiClient = (
20
25
  // url: req.path,
21
26
  data,
22
27
  params: req.query,
28
+ headers: {
29
+ ContentType: type
30
+ }
23
31
  })
24
32
  .then((r) => r.data)
25
33
  },
@@ -1,9 +1,11 @@
1
+ import {PlatformHelper} from "./platform/types";
1
2
 
2
3
 
3
- export const convertToFormData = (payload: Record<string, any>): FormData => {
4
- const formData = new FormData()
4
+ export const convertToFormData = (payload: Record<string, any>, platform: PlatformHelper): FormData => {
5
+ const formData = platform.newFormData()
6
+
5
7
  const addProp = (key: string, val: any) => {
6
- if (Array.isArray(val) || val instanceof FileList) {
8
+ if (Array.isArray(val) || platform.isFileList(val)) {
7
9
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
8
10
  // @ts-ignore - seems that FileList is iterable despite the warning
9
11
  // TODO change to other iteration method if this is not true
@@ -13,11 +15,16 @@ export const convertToFormData = (payload: Record<string, any>): FormData => {
13
15
  } else if (
14
16
  typeof val === 'object' &&
15
17
  val != null &&
16
- !(val instanceof File)
18
+ !platform.isFile(val)
17
19
  ) {
18
- throw new Error('Object serialization into FormData not supported')
20
+ throw new Error(`Object serialization into FormData not supported for object: ${val}`)
19
21
  } else {
20
- formData.append(key, val)
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
+ }
21
28
  }
22
29
  }
23
30
 
@@ -0,0 +1,22 @@
1
+ import {PlatformHelper} from "./types";
2
+
3
+ export class BrowserPlatFormHelper implements PlatformHelper {
4
+
5
+ isFile = obj =>
6
+ obj instanceof File
7
+
8
+ getFileAndName = (obj) => {
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 =>
19
+ obj instanceof FileList
20
+ // @ts-ignore
21
+ newFormData = () => new FormData()
22
+ }
@@ -0,0 +1,3 @@
1
+ export {BrowserPlatFormHelper} from "./browser";
2
+ export {NodePlatFormHelper} from "./node";
3
+
@@ -0,0 +1,34 @@
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 => {
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) => {
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 => {
29
+ // No FileList in node?
30
+ return false
31
+ }
32
+ // @ts-ignore
33
+ newFormData = (): FormData => new NodeFormData()
34
+ }
@@ -0,0 +1,9 @@
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
+ }
package/tsconfig.json CHANGED
@@ -1,3 +1,9 @@
1
1
  {
2
+ "compilerOptions": {
3
+ "moduleResolution": "node"
4
+ },
2
5
  "extends": "../../tsconfig.json",
6
+ "include": [
7
+ "src/**/*.ts"
8
+ ]
3
9
  }
@@ -1,3 +0,0 @@
1
- import { AxiosInstance } from 'axios';
2
- import { HttpClient } from './mytyp';
3
- export declare const createApiClient: (axios: AxiosInstance) => HttpClient;
@@ -1 +0,0 @@
1
- export declare const convertToFormData: (payload: Record<string, any>) => FormData;
@@ -1 +0,0 @@
1
- export { createApiClient } from './AxiosOpenApiHttpClient';
@@ -1,21 +0,0 @@
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
- };
@@ -1 +0,0 @@
1
- export declare const serializeQuery: (obj: object) => string;
@@ -1 +0,0 @@
1
- export declare const isEven: (x: number) => boolean;
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};