@appsemble/node-utils 0.20.9 → 0.20.12

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.
Files changed (121) hide show
  1. package/{dist/AppsembleError.d.ts → AppsembleError.d.ts} +1 -1
  2. package/{dist/AppsembleError.js → AppsembleError.js} +1 -1
  3. package/AppsembleError.js.map +1 -0
  4. package/AppsembleError.test.d.ts +1 -0
  5. package/AppsembleError.test.js +8 -0
  6. package/AppsembleError.test.js.map +1 -0
  7. package/AppsembleError.test.ts +6 -0
  8. package/AppsembleError.ts +16 -0
  9. package/__fixtures__/hello.txt +1 -0
  10. package/__fixtures__/invalid-json.json +3 -0
  11. package/__fixtures__/json.json +1 -0
  12. package/__fixtures__/test/bar.txt +0 -0
  13. package/__fixtures__/test/foo.txt +0 -0
  14. package/__fixtures__/test/z/baz.txt +0 -0
  15. package/__fixtures__/test/z/fooz.txt +0 -0
  16. package/__fixtures__/yaml.yaml +1 -0
  17. package/__mocks__/os.d.ts +5 -0
  18. package/__mocks__/os.js +8 -0
  19. package/__mocks__/os.js.map +1 -0
  20. package/__mocks__/os.ts +10 -0
  21. package/{dist/basicAuth.d.ts → basicAuth.d.ts} +2 -2
  22. package/{dist/basicAuth.js → basicAuth.js} +2 -2
  23. package/basicAuth.js.map +1 -0
  24. package/basicAuth.test.d.ts +1 -0
  25. package/basicAuth.test.js +8 -0
  26. package/basicAuth.test.js.map +1 -0
  27. package/basicAuth.test.ts +6 -0
  28. package/basicAuth.ts +10 -0
  29. package/changed/added/.gitkeep +0 -0
  30. package/changed/changed/.gitkeep +0 -0
  31. package/changed/deprecated/.gitkeep +0 -0
  32. package/changed/fixed/.gitkeep +0 -0
  33. package/changed/removed/.gitkeep +0 -0
  34. package/changed/security/.gitkeep +0 -0
  35. package/{dist/formData.d.ts → formData.d.ts} +1 -1
  36. package/{dist/formData.js → formData.js} +1 -1
  37. package/formData.js.map +1 -0
  38. package/formData.test.d.ts +1 -0
  39. package/formData.test.js +94 -0
  40. package/formData.test.js.map +1 -0
  41. package/formData.test.ts +113 -0
  42. package/formData.ts +37 -0
  43. package/{dist/fs.d.ts → fs.d.ts} +10 -10
  44. package/{dist/fs.js → fs.js} +16 -16
  45. package/fs.js.map +1 -0
  46. package/fs.test.d.ts +1 -0
  47. package/fs.test.js +76 -0
  48. package/fs.test.js.map +1 -0
  49. package/fs.test.ts +120 -0
  50. package/fs.ts +144 -0
  51. package/{dist/getWorkspaces.d.ts → getWorkspaces.d.ts} +1 -1
  52. package/{dist/getWorkspaces.js → getWorkspaces.js} +3 -3
  53. package/getWorkspaces.js.map +1 -0
  54. package/getWorkspaces.ts +23 -0
  55. package/{dist/handleError.d.ts → handleError.d.ts} +3 -3
  56. package/{dist/handleError.js → handleError.js} +3 -3
  57. package/handleError.js.map +1 -0
  58. package/handleError.test.d.ts +1 -0
  59. package/handleError.test.js +34 -0
  60. package/handleError.test.js.map +1 -0
  61. package/handleError.test.ts +38 -0
  62. package/handleError.ts +26 -0
  63. package/{dist/index.d.ts → index.d.ts} +0 -0
  64. package/{dist/index.js → index.js} +0 -0
  65. package/index.js.map +1 -0
  66. package/index.ts +11 -0
  67. package/{dist/interceptors.d.ts → interceptors.d.ts} +5 -5
  68. package/{dist/interceptors.js → interceptors.js} +5 -5
  69. package/interceptors.js.map +1 -0
  70. package/interceptors.test.d.ts +1 -0
  71. package/interceptors.test.js +93 -0
  72. package/interceptors.test.js.map +1 -0
  73. package/interceptors.test.ts +108 -0
  74. package/interceptors.ts +81 -0
  75. package/jest.config.js +1 -0
  76. package/jest.setup.d.ts +1 -0
  77. package/jest.setup.js +9 -0
  78. package/jest.setup.js.map +1 -0
  79. package/jest.setup.ts +7 -0
  80. package/{dist/logger.d.ts → logger.d.ts} +2 -2
  81. package/{dist/logger.js → logger.js} +6 -6
  82. package/logger.js.map +1 -0
  83. package/logger.test.d.ts +1 -0
  84. package/logger.test.js +38 -0
  85. package/logger.test.js.map +1 -0
  86. package/logger.test.ts +43 -0
  87. package/logger.ts +130 -0
  88. package/{dist/loggerMiddleware.d.ts → loggerMiddleware.d.ts} +0 -0
  89. package/{dist/loggerMiddleware.js → loggerMiddleware.js} +18 -15
  90. package/loggerMiddleware.js.map +1 -0
  91. package/loggerMiddleware.test.d.ts +1 -0
  92. package/loggerMiddleware.test.js +130 -0
  93. package/loggerMiddleware.test.js.map +1 -0
  94. package/loggerMiddleware.test.ts +185 -0
  95. package/loggerMiddleware.ts +93 -0
  96. package/package.json +7 -6
  97. package/{dist/readFileOrString.d.ts → readFileOrString.d.ts} +1 -1
  98. package/{dist/readFileOrString.js → readFileOrString.js} +3 -3
  99. package/readFileOrString.js.map +1 -0
  100. package/readFileOrString.test.d.ts +1 -0
  101. package/readFileOrString.test.js +13 -0
  102. package/readFileOrString.test.js.map +1 -0
  103. package/readFileOrString.test.ts +12 -0
  104. package/readFileOrString.ts +20 -0
  105. package/{dist/testFixtures.d.ts → testFixtures.d.ts} +5 -5
  106. package/{dist/testFixtures.js → testFixtures.js} +5 -4
  107. package/testFixtures.js.map +1 -0
  108. package/testFixtures.ts +58 -0
  109. package/tsconfig.json +6 -0
  110. package/dist/AppsembleError.js.map +0 -1
  111. package/dist/basicAuth.js.map +0 -1
  112. package/dist/formData.js.map +0 -1
  113. package/dist/fs.js.map +0 -1
  114. package/dist/getWorkspaces.js.map +0 -1
  115. package/dist/handleError.js.map +0 -1
  116. package/dist/index.js.map +0 -1
  117. package/dist/interceptors.js.map +0 -1
  118. package/dist/logger.js.map +0 -1
  119. package/dist/loggerMiddleware.js.map +0 -1
  120. package/dist/readFileOrString.js.map +0 -1
  121. package/dist/testFixtures.js.map +0 -1
@@ -6,7 +6,7 @@
6
6
  */
7
7
  export declare class AppsembleError extends Error {
8
8
  /**
9
- * @param message - The error message to show to the user.
9
+ * @param message The error message to show to the user.
10
10
  */
11
11
  constructor(message: string);
12
12
  }
@@ -9,7 +9,7 @@ exports.AppsembleError = void 0;
9
9
  */
10
10
  class AppsembleError extends Error {
11
11
  /**
12
- * @param message - The error message to show to the user.
12
+ * @param message The error message to show to the user.
13
13
  */
14
14
  constructor(message) {
15
15
  super(message);
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AppsembleError.js","sourceRoot":"","sources":["AppsembleError.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,MAAa,cAAe,SAAQ,KAAK;IACvC;;OAEG;IACH,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;CACF;AATD,wCASC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const AppsembleError_1 = require("./AppsembleError");
4
+ it('should define AppsembleError as its name', () => {
5
+ const error = new AppsembleError_1.AppsembleError('message');
6
+ expect(error.name).toBe('AppsembleError');
7
+ });
8
+ //# sourceMappingURL=AppsembleError.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AppsembleError.test.js","sourceRoot":"","sources":["AppsembleError.test.ts"],"names":[],"mappings":";;AAAA,qDAAkD;AAElD,EAAE,CAAC,0CAA0C,EAAE,GAAG,EAAE;IAClD,MAAM,KAAK,GAAG,IAAI,+BAAc,CAAC,SAAS,CAAC,CAAC;IAC5C,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;AAC5C,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { AppsembleError } from './AppsembleError';
2
+
3
+ it('should define AppsembleError as its name', () => {
4
+ const error = new AppsembleError('message');
5
+ expect(error.name).toBe('AppsembleError');
6
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Used for throwing known Appsemble errors.
3
+ *
4
+ * This is used when the normal control flow needs to be aborted, but for reasons known. When an
5
+ * instance of `AppsembleError` is thrown, the message will be logged, but not the stack trace.
6
+ */
7
+ export class AppsembleError extends Error {
8
+ /**
9
+ * @param message The error message to show to the user.
10
+ */
11
+ constructor(message: string) {
12
+ super(message);
13
+ this.name = 'AppsembleError';
14
+ Error.captureStackTrace(this, this.constructor);
15
+ }
16
+ }
@@ -0,0 +1 @@
1
+ Hello world!
@@ -0,0 +1,3 @@
1
+ {
2
+ // comment
3
+ }
@@ -0,0 +1 @@
1
+ { "json": "json" }
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ yaml: yaml
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This mock ensures the values of `os` are consistent across platforms in tests.
3
+ */
4
+ declare const _default: any;
5
+ export = _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const actual = jest.requireActual('os');
3
+ module.exports = {
4
+ ...actual,
5
+ type: () => 'Linux',
6
+ arch: () => 'x64',
7
+ };
8
+ //# sourceMappingURL=os.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os.js","sourceRoot":"","sources":["os.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;AAKxC,iBAAS;IACP,GAAG,MAAM;IACT,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO;IACnB,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK;CAClB,CAAC"}
@@ -0,0 +1,10 @@
1
+ const actual = jest.requireActual('os');
2
+
3
+ /**
4
+ * This mock ensures the values of `os` are consistent across platforms in tests.
5
+ */
6
+ export = {
7
+ ...actual,
8
+ type: () => 'Linux',
9
+ arch: () => 'x64',
10
+ };
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Create a basic auth authorization header from a username and password.
3
3
  *
4
- * @param username - The username to serialize.
5
- * @param password - The password to serialize.
4
+ * @param username The username to serialize.
5
+ * @param password The password to serialize.
6
6
  * @returns An HTTP basic auth authorization header.
7
7
  */
8
8
  export declare function basicAuth(username: string, password: string): string;
@@ -4,8 +4,8 @@ exports.basicAuth = void 0;
4
4
  /**
5
5
  * Create a basic auth authorization header from a username and password.
6
6
  *
7
- * @param username - The username to serialize.
8
- * @param password - The password to serialize.
7
+ * @param username The username to serialize.
8
+ * @param password The password to serialize.
9
9
  * @returns An HTTP basic auth authorization header.
10
10
  */
11
11
  function basicAuth(username, password) {
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basicAuth.js","sourceRoot":"","sources":["basicAuth.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,SAAgB,SAAS,CAAC,QAAgB,EAAE,QAAgB;IAC1D,OAAO,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,IAAI,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;AAC9E,CAAC;AAFD,8BAEC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const basicAuth_1 = require("./basicAuth");
4
+ it('should serialize credentials', () => {
5
+ const result = (0, basicAuth_1.basicAuth)('me', 'password1');
6
+ expect(result).toBe('Basic bWU6cGFzc3dvcmQx');
7
+ });
8
+ //# sourceMappingURL=basicAuth.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basicAuth.test.js","sourceRoot":"","sources":["basicAuth.test.ts"],"names":[],"mappings":";;AAAA,2CAAwC;AAExC,EAAE,CAAC,8BAA8B,EAAE,GAAG,EAAE;IACtC,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAC5C,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;AAChD,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { basicAuth } from './basicAuth';
2
+
3
+ it('should serialize credentials', () => {
4
+ const result = basicAuth('me', 'password1');
5
+ expect(result).toBe('Basic bWU6cGFzc3dvcmQx');
6
+ });
package/basicAuth.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Create a basic auth authorization header from a username and password.
3
+ *
4
+ * @param username The username to serialize.
5
+ * @param password The password to serialize.
6
+ * @returns An HTTP basic auth authorization header.
7
+ */
8
+ export function basicAuth(username: string, password: string): string {
9
+ return `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
10
+ }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -2,7 +2,7 @@ import FormData from 'form-data';
2
2
  /**
3
3
  * A convenience function for creating form data.
4
4
  *
5
- * @param data - A key / value record of fields to append to the form data. Booleans, numbersm
5
+ * @param data A key / value record of fields to append to the form data. Booleans, numbers,
6
6
  * strings, and `null` will be added to the form data raw. Objects will be transformed to JSON
7
7
  * objects. Arrays will be iterated over and every item will be appended for the given key.
8
8
  * @returns The data represented as form data.
@@ -24,7 +24,7 @@ function appendFormData(key, value, form) {
24
24
  /**
25
25
  * A convenience function for creating form data.
26
26
  *
27
- * @param data - A key / value record of fields to append to the form data. Booleans, numbersm
27
+ * @param data A key / value record of fields to append to the form data. Booleans, numbers,
28
28
  * strings, and `null` will be added to the form data raw. Objects will be transformed to JSON
29
29
  * objects. Arrays will be iterated over and every item will be appended for the given key.
30
30
  * @returns The data represented as form data.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formData.js","sourceRoot":"","sources":["formData.ts"],"names":[],"mappings":";;;;;;AAAA,2BAAgC;AAEhC,0DAAiC;AAEjC,SAAS,cAAc,CAAC,GAAW,EAAE,KAAU,EAAE,IAAc;IAC7D,IAAI,KAAK,KAAK,SAAS,EAAE;QACvB,OAAO;KACR;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACxB,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;YACvB,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;SAChC;KACF;SAAM;QACL,IAAI,CAAC,MAAM,CACT,GAAG,EACH,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,eAAU;YAChF,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAC1B,CAAC;KACH;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,cAAc,CAAC,IAAyB;IACtD,MAAM,IAAI,GAAG,IAAI,mBAAQ,EAAE,CAAC;IAC5B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QAC/C,cAAc,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;KAClC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAND,wCAMC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const formData_1 = require("./formData");
4
+ describe('createFormData', () => {
5
+ it('should handle strings', () => {
6
+ const form = (0, formData_1.createFormData)({ foo: 'bar' });
7
+ const boundary = form.getBoundary();
8
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
9
+ Content-Disposition: form-data; name="foo"\r
10
+ \r
11
+ bar\r
12
+ --${boundary}--\r
13
+ `);
14
+ });
15
+ it('should handle numbers', () => {
16
+ const form = (0, formData_1.createFormData)({ answer: 42 });
17
+ const boundary = form.getBoundary();
18
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
19
+ Content-Disposition: form-data; name="answer"\r
20
+ \r
21
+ 42\r
22
+ --${boundary}--\r
23
+ `);
24
+ });
25
+ it('should handle booleans', () => {
26
+ const form = (0, formData_1.createFormData)({ really: true });
27
+ const boundary = form.getBoundary();
28
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
29
+ Content-Disposition: form-data; name="really"\r
30
+ \r
31
+ true\r
32
+ --${boundary}--\r
33
+ `);
34
+ });
35
+ it('should handle buffers', () => {
36
+ const form = (0, formData_1.createFormData)({ buf: Buffer.from('fer') });
37
+ const boundary = form.getBoundary();
38
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
39
+ Content-Disposition: form-data; name="buf"\r
40
+ Content-Type: application/octet-stream\r
41
+ \r
42
+ fer\r
43
+ --${boundary}--\r
44
+ `);
45
+ });
46
+ it('should handle null', () => {
47
+ const form = (0, formData_1.createFormData)({ really: true });
48
+ const boundary = form.getBoundary();
49
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
50
+ Content-Disposition: form-data; name="really"\r
51
+ \r
52
+ true\r
53
+ --${boundary}--\r
54
+ `);
55
+ });
56
+ it('should ignore undefined', () => {
57
+ const form = (0, formData_1.createFormData)({ defined: undefined });
58
+ const boundary = form.getBoundary();
59
+ expect(String(form.getBuffer())).toBe(`--${boundary}--\r\n`);
60
+ });
61
+ it('should convert objects to JSON', () => {
62
+ const form = (0, formData_1.createFormData)({ deeply: { nested: 'data' } });
63
+ const boundary = form.getBoundary();
64
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
65
+ Content-Disposition: form-data; name="deeply"\r
66
+ \r
67
+ {"nested":"data"}\r
68
+ --${boundary}--\r
69
+ `);
70
+ });
71
+ it('should append multiple entries if the input is an array', () => {
72
+ const form = (0, formData_1.createFormData)({ array: [42, 'bananas', null, { fruit: true }] });
73
+ const boundary = form.getBoundary();
74
+ expect(String(form.getBuffer())).toBe(`--${boundary}\r
75
+ Content-Disposition: form-data; name="array"\r
76
+ \r
77
+ 42\r
78
+ --${boundary}\r
79
+ Content-Disposition: form-data; name="array"\r
80
+ \r
81
+ bananas\r
82
+ --${boundary}\r
83
+ Content-Disposition: form-data; name="array"\r
84
+ \r
85
+ null\r
86
+ --${boundary}\r
87
+ Content-Disposition: form-data; name="array"\r
88
+ \r
89
+ {"fruit":true}\r
90
+ --${boundary}--\r
91
+ `);
92
+ });
93
+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CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,IAAI,CACnC,KAAK,QAAQ;;;;IAIf,QAAQ;CACX,CACI,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yDAAyD,EAAE,GAAG,EAAE;QACjE,MAAM,IAAI,GAAG,IAAA,yBAAc,EAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;QAC/E,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACpC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,IAAI,CACnC,KAAK,QAAQ;;;;IAIf,QAAQ;;;;IAIR,QAAQ;;;;IAIR,QAAQ;;;;IAIR,QAAQ;CACX,CACI,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,113 @@
1
+ import { createFormData } from './formData';
2
+
3
+ describe('createFormData', () => {
4
+ it('should handle strings', () => {
5
+ const form = createFormData({ foo: 'bar' });
6
+ const boundary = form.getBoundary();
7
+ expect(String(form.getBuffer())).toBe(
8
+ `--${boundary}\r
9
+ Content-Disposition: form-data; name="foo"\r
10
+ \r
11
+ bar\r
12
+ --${boundary}--\r
13
+ `,
14
+ );
15
+ });
16
+
17
+ it('should handle numbers', () => {
18
+ const form = createFormData({ answer: 42 });
19
+ const boundary = form.getBoundary();
20
+ expect(String(form.getBuffer())).toBe(
21
+ `--${boundary}\r
22
+ Content-Disposition: form-data; name="answer"\r
23
+ \r
24
+ 42\r
25
+ --${boundary}--\r
26
+ `,
27
+ );
28
+ });
29
+
30
+ it('should handle booleans', () => {
31
+ const form = createFormData({ really: true });
32
+ const boundary = form.getBoundary();
33
+ expect(String(form.getBuffer())).toBe(
34
+ `--${boundary}\r
35
+ Content-Disposition: form-data; name="really"\r
36
+ \r
37
+ true\r
38
+ --${boundary}--\r
39
+ `,
40
+ );
41
+ });
42
+
43
+ it('should handle buffers', () => {
44
+ const form = createFormData({ buf: Buffer.from('fer') });
45
+ const boundary = form.getBoundary();
46
+ expect(String(form.getBuffer())).toBe(
47
+ `--${boundary}\r
48
+ Content-Disposition: form-data; name="buf"\r
49
+ Content-Type: application/octet-stream\r
50
+ \r
51
+ fer\r
52
+ --${boundary}--\r
53
+ `,
54
+ );
55
+ });
56
+
57
+ it('should handle null', () => {
58
+ const form = createFormData({ really: true });
59
+ const boundary = form.getBoundary();
60
+ expect(String(form.getBuffer())).toBe(
61
+ `--${boundary}\r
62
+ Content-Disposition: form-data; name="really"\r
63
+ \r
64
+ true\r
65
+ --${boundary}--\r
66
+ `,
67
+ );
68
+ });
69
+
70
+ it('should ignore undefined', () => {
71
+ const form = createFormData({ defined: undefined });
72
+ const boundary = form.getBoundary();
73
+ expect(String(form.getBuffer())).toBe(`--${boundary}--\r\n`);
74
+ });
75
+
76
+ it('should convert objects to JSON', () => {
77
+ const form = createFormData({ deeply: { nested: 'data' } });
78
+ const boundary = form.getBoundary();
79
+ expect(String(form.getBuffer())).toBe(
80
+ `--${boundary}\r
81
+ Content-Disposition: form-data; name="deeply"\r
82
+ \r
83
+ {"nested":"data"}\r
84
+ --${boundary}--\r
85
+ `,
86
+ );
87
+ });
88
+
89
+ it('should append multiple entries if the input is an array', () => {
90
+ const form = createFormData({ array: [42, 'bananas', null, { fruit: true }] });
91
+ const boundary = form.getBoundary();
92
+ expect(String(form.getBuffer())).toBe(
93
+ `--${boundary}\r
94
+ Content-Disposition: form-data; name="array"\r
95
+ \r
96
+ 42\r
97
+ --${boundary}\r
98
+ Content-Disposition: form-data; name="array"\r
99
+ \r
100
+ bananas\r
101
+ --${boundary}\r
102
+ Content-Disposition: form-data; name="array"\r
103
+ \r
104
+ null\r
105
+ --${boundary}\r
106
+ Content-Disposition: form-data; name="array"\r
107
+ \r
108
+ {"fruit":true}\r
109
+ --${boundary}--\r
110
+ `,
111
+ );
112
+ });
113
+ });
package/formData.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { ReadStream } from 'fs';
2
+
3
+ import FormData from 'form-data';
4
+
5
+ function appendFormData(key: string, value: any, form: FormData): void {
6
+ if (value === undefined) {
7
+ return;
8
+ }
9
+ if (Array.isArray(value)) {
10
+ for (const val of value) {
11
+ appendFormData(key, val, form);
12
+ }
13
+ } else {
14
+ form.append(
15
+ key,
16
+ typeof value === 'string' || Buffer.isBuffer(value) || value instanceof ReadStream
17
+ ? value
18
+ : JSON.stringify(value),
19
+ );
20
+ }
21
+ }
22
+
23
+ /**
24
+ * A convenience function for creating form data.
25
+ *
26
+ * @param data A key / value record of fields to append to the form data. Booleans, numbers,
27
+ * strings, and `null` will be added to the form data raw. Objects will be transformed to JSON
28
+ * objects. Arrays will be iterated over and every item will be appended for the given key.
29
+ * @returns The data represented as form data.
30
+ */
31
+ export function createFormData(data: Record<string, any>): FormData {
32
+ const form = new FormData();
33
+ for (const [key, value] of Object.entries(data)) {
34
+ appendFormData(key, value, form);
35
+ }
36
+ return form;
37
+ }
@@ -5,15 +5,15 @@ import { Promisable } from 'type-fest';
5
5
  /**
6
6
  * Test if the error is a NodeJS errno exception.
7
7
  *
8
- * @param error - The value to check
9
- * @param code - If specified, theck if the code matches
8
+ * @param error The value to check
9
+ * @param code If specified, theck if the code matches
10
10
  * @returns Whether or not the error is a NodeJS errno exception.
11
11
  */
12
12
  export declare function isErrno(error: unknown, code?: string): error is NodeJS.ErrnoException;
13
13
  /**
14
14
  * Read the content of a JSON or YAML file.
15
15
  *
16
- * @param path - The path to the file to read.
16
+ * @param path The path to the file to read.
17
17
  * @returns A tuple of the parsed content and the content as a string.
18
18
  */
19
19
  export declare function readData<R>(path: string): Promise<[R, string]>;
@@ -34,9 +34,9 @@ interface WriteDataOptions {
34
34
  *
35
35
  * If `prettier` is available, the content is also formatted.
36
36
  *
37
- * @param path - The file path to write the data to.
38
- * @param data - The data to write to the file.
39
- * @param options - Additional options for processing the data.
37
+ * @param path The file path to write the data to.
38
+ * @param data The data to write to the file.
39
+ * @param options Additional options for processing the data.
40
40
  * @returns The formatted content.
41
41
  */
42
42
  export declare function writeData(path: string, data: unknown, { compare, sort }?: WriteDataOptions): Promise<string>;
@@ -47,10 +47,10 @@ interface OpenDirSafeOptions {
47
47
  /**
48
48
  * Read the contents of a directory.
49
49
  *
50
- * @param directory - The path of the directory to open.
51
- * @param onFile - A callback which will get called for every file. This will be called with the
52
- * full file path as its first argument and its `Dirent` object as the second argument.
53
- * @param options - Additional options.
50
+ * @param directory The path of the directory to open.
51
+ * @param onFile A callback which will get called for every file. This will be called with the full
52
+ * file path as its first argument and its `Dirent` object as the second argument.
53
+ * @param options Additional options.
54
54
  */
55
55
  export declare function opendirSafe(directory: string, onFile: (fullpath: string, stat: Dirent) => Promisable<void>, options?: OpenDirSafeOptions): Promise<void>;
56
56
  export {};
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.opendirSafe = exports.writeData = exports.readData = exports.isErrno = void 0;
30
- const fs_1 = require("fs");
30
+ const promises_1 = require("fs/promises");
31
31
  const path_1 = require("path");
32
32
  const utils_1 = require("@appsemble/utils");
33
33
  const parse_json_1 = __importDefault(require("parse-json"));
@@ -37,8 +37,8 @@ const _1 = require(".");
37
37
  /**
38
38
  * Test if the error is a NodeJS errno exception.
39
39
  *
40
- * @param error - The value to check
41
- * @param code - If specified, theck if the code matches
40
+ * @param error The value to check
41
+ * @param code If specified, theck if the code matches
42
42
  * @returns Whether or not the error is a NodeJS errno exception.
43
43
  */
44
44
  function isErrno(error, code) {
@@ -55,14 +55,14 @@ exports.isErrno = isErrno;
55
55
  /**
56
56
  * Read the content of a JSON or YAML file.
57
57
  *
58
- * @param path - The path to the file to read.
58
+ * @param path The path to the file to read.
59
59
  * @returns A tuple of the parsed content and the content as a string.
60
60
  */
61
61
  async function readData(path) {
62
62
  let content;
63
63
  const ext = (0, path_1.extname)(path);
64
64
  try {
65
- content = await fs_1.promises.readFile(path, 'utf8');
65
+ content = await (0, promises_1.readFile)(path, 'utf8');
66
66
  }
67
67
  catch {
68
68
  throw new _1.AppsembleError(`Error reading file ${path}`);
@@ -86,9 +86,9 @@ exports.readData = readData;
86
86
  *
87
87
  * If `prettier` is available, the content is also formatted.
88
88
  *
89
- * @param path - The file path to write the data to.
90
- * @param data - The data to write to the file.
91
- * @param options - Additional options for processing the data.
89
+ * @param path The file path to write the data to.
90
+ * @param data The data to write to the file.
91
+ * @param options Additional options for processing the data.
92
92
  * @returns The formatted content.
93
93
  */
94
94
  async function writeData(path, data, { compare = utils_1.compareStrings, sort = true } = {}) {
@@ -107,23 +107,23 @@ async function writeData(path, data, { compare = utils_1.compareStrings, sort =
107
107
  buffer =
108
108
  ext === '.yml' || ext === '.yaml' ? (0, yaml_1.stringify)(sorted) : JSON.stringify(sorted, undefined, 2);
109
109
  }
110
- await fs_1.promises.mkdir((0, path_1.dirname)(path), { recursive: true });
111
- await fs_1.promises.writeFile(path, buffer);
110
+ await (0, promises_1.mkdir)((0, path_1.dirname)(path), { recursive: true });
111
+ await (0, promises_1.writeFile)(path, buffer);
112
112
  return buffer;
113
113
  }
114
114
  exports.writeData = writeData;
115
115
  /**
116
116
  * Read the contents of a directory.
117
117
  *
118
- * @param directory - The path of the directory to open.
119
- * @param onFile - A callback which will get called for every file. This will be called with the
120
- * full file path as its first argument and its `Dirent` object as the second argument.
121
- * @param options - Additional options.
118
+ * @param directory The path of the directory to open.
119
+ * @param onFile A callback which will get called for every file. This will be called with the full
120
+ * file path as its first argument and its `Dirent` object as the second argument.
121
+ * @param options Additional options.
122
122
  */
123
123
  async function opendirSafe(directory, onFile, options = {}) {
124
124
  let stats;
125
125
  try {
126
- stats = await fs_1.promises.stat(directory);
126
+ stats = await (0, promises_1.stat)(directory);
127
127
  }
128
128
  catch (err) {
129
129
  if (options.allowMissing && isErrno(err, 'ENOENT')) {
@@ -134,7 +134,7 @@ async function opendirSafe(directory, onFile, options = {}) {
134
134
  if (!stats.isDirectory()) {
135
135
  throw new _1.AppsembleError(`Expected ${directory} to be a directory`);
136
136
  }
137
- const dir = await fs_1.promises.opendir(directory);
137
+ const dir = await (0, promises_1.opendir)(directory);
138
138
  for await (const file of dir) {
139
139
  const fullPath = (0, path_1.join)(directory, file.name);
140
140
  await onFile(fullPath, file);
package/fs.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs.js","sourceRoot":"","sources":["fs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0CAAwE;AACxE,+BAA8C;AAE9C,4CAAkD;AAClD,4DAAmC;AACnC,0DAAiC;AAEjC,+BAAwC;AAExC,wBAAmC;AAEnC;;;;;;GAMG;AACH,SAAgB,OAAO,CAAC,KAAc,EAAE,IAAa;IACnD,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACvC,OAAO,KAAK,CAAC;KACd;IACD,MAAM,GAAG,GAAG,KAA8B,CAAC;IAC3C,IAAI,IAAI,EAAE;QACR,OAAO,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC;KAC1B;IACD,OAAO,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC;AACtC,CAAC;AATD,0BASC;AAED;;;;;GAKG;AACI,KAAK,UAAU,QAAQ,CAAI,IAAY;IAC5C,IAAI,OAAe,CAAC;IACpB,MAAM,GAAG,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;IAC1B,IAAI;QACF,OAAO,GAAG,MAAM,IAAA,mBAAQ,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;KACxC;IAAC,MAAM;QACN,MAAM,IAAI,iBAAc,CAAC,sBAAsB,IAAI,EAAE,CAAC,CAAC;KACxD;IACD,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,MAAM,IAAI,GA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