@trayio/commons 4.47.0 → 4.49.0

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 +1 @@
1
- {"version":3,"file":"FakeFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/FakeFileStorage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAIvC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEtD,qBAAa,eAAgB,YAAW,WAAW;IAClD,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;;IAMzB,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAY7C,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkBlD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAYzD,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAI1D,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;CAgB7D"}
1
+ {"version":3,"file":"FakeFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/FakeFileStorage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAIvC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEtD,qBAAa,eAAgB,YAAW,WAAW;IAClD,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;;IAMzB,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAY7C,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkBlD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAYzD,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAI1D,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;CAmB7D"}
@@ -34,7 +34,7 @@ class FakeFileStorage {
34
34
  this.files = new Map();
35
35
  }
36
36
  write(file) {
37
- return (0, function_1.pipe)(TE.fromEither(E.tryCatch(() => this.files.set(file.key, file), (error) => new Error(error))), TE.map(() => file));
37
+ return (0, function_1.pipe)(TE.fromEither(E.tryCatch(() => this.files.set(file.reference.key, file), (error) => new Error(error))), TE.map(() => file));
38
38
  }
39
39
  read(fileName) {
40
40
  return (0, function_1.pipe)(TE.fromEither(E.tryCatch(() => this.files.get(fileName), (error) => new Error(error))), TE.chain((file) => {
@@ -52,8 +52,11 @@ class FakeFileStorage {
52
52
  }
53
53
  createDirectory(key) {
54
54
  return (0, function_1.pipe)(TE.fromEither(E.tryCatch(() => this.files.set(key, {
55
- key,
56
- metadata: {},
55
+ reference: {
56
+ _tag: 'FileDescriptor',
57
+ key,
58
+ metadata: { name: key },
59
+ },
57
60
  content: stream_1.Readable.from(''),
58
61
  }), (error) => new Error(error))), TE.map(() => undefined));
59
62
  }
@@ -5,24 +5,26 @@ import * as t from 'io-ts';
5
5
  import { Option } from 'fp-ts/lib/Option';
6
6
  import { DynamicObject } from '../dynamictype/DynamicType';
7
7
  export type FileMetadata = {
8
+ name: string;
8
9
  size?: number;
9
10
  contentType?: string;
10
11
  };
11
12
  export declare const fileMetadataDescriptor: t.Type<FileMetadata, unknown>;
13
+ export declare const fileDescriptorDescriptor: t.Type<FileDescriptor, unknown>;
12
14
  export type File = {
13
- key: string;
14
- metadata: FileMetadata;
15
+ reference: FileDescriptor;
15
16
  content: Readable;
16
17
  };
17
- export type FileReference = {
18
- _tag: 'FileReference';
18
+ export type FileDescriptor = {
19
+ _tag: 'FileDescriptor';
19
20
  key: string;
21
+ metadata: FileMetadata;
20
22
  };
21
- export interface FileReferenceInterface {
22
- isFileReference(value: unknown): value is FileReference;
23
- fromFile(file: File): FileReference;
23
+ export interface FileDescriptorInterface {
24
+ isFileDescriptor(value: unknown): value is FileDescriptor;
25
+ fromFile(file: File): FileDescriptor;
24
26
  }
25
- export declare const FileReference: FileReferenceInterface;
27
+ export declare const FileDescriptor: FileDescriptorInterface;
26
28
  export type SharedUrl = {
27
29
  url: string;
28
30
  expires?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"File.d.ts","sourceRoot":"","sources":["../../src/file/File.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,MAAM,MAAM,YAAY,GAAG;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AACF,eAAO,MAAM,sBAAsB,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAG/D,CAAC;AAKH,MAAM,MAAM,IAAI,GAAG;IAElB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,QAAQ,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,a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MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAClD,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAC3D"}
package/dist/file/File.js CHANGED
@@ -23,25 +23,38 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.FileReference = exports.fileMetadataDescriptor = void 0;
26
+ exports.FileDescriptor = exports.fileDescriptorDescriptor = exports.fileMetadataDescriptor = void 0;
27
27
  const t = __importStar(require("io-ts"));
28
- exports.fileMetadataDescriptor = t.partial({
29
- size: t.number,
30
- contentType: t.string,
28
+ exports.fileMetadataDescriptor = t.intersection([
29
+ t.type({
30
+ name: t.string,
31
+ }),
32
+ t.partial({
33
+ size: t.number,
34
+ contentType: t.string,
35
+ }),
36
+ ]);
37
+ exports.fileDescriptorDescriptor = t.type({
38
+ _tag: t.literal('FileDescriptor'),
39
+ key: t.string,
40
+ metadata: exports.fileMetadataDescriptor,
31
41
  });
32
- exports.FileReference = {
33
- isFileReference(value) {
42
+ exports.FileDescriptor = {
43
+ isFileDescriptor(value) {
34
44
  return (typeof value === 'object' &&
35
45
  value !== null &&
36
46
  '_tag' in value &&
37
- value._tag === 'FileReference' &&
47
+ value._tag === 'FileDescriptor' &&
38
48
  'key' in value &&
39
- typeof value.key === 'string');
49
+ typeof value.key === 'string' &&
50
+ 'metadata' in value &&
51
+ exports.fileMetadataDescriptor.is(value.metadata));
40
52
  },
41
53
  fromFile(file) {
42
54
  return {
43
- _tag: 'FileReference',
44
- key: file.key,
55
+ _tag: 'FileDescriptor',
56
+ key: file.reference.key,
57
+ metadata: file.reference.metadata,
45
58
  };
46
59
  },
47
60
  };
@@ -1 +1 @@
1
- {"version":3,"file":"FileStorage.abstract.test.d.ts","sourceRoot":"","sources":["../../src/file/FileStorage.abstract.test.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAQ,MAAM,QAAQ,CAAC;AAE3C,eAAO,MAAM,eAAe,gBAAiB,WAAW,SAwFvD,CAAC"}
1
+ {"version":3,"file":"FileStorage.abstract.test.d.ts","sourceRoot":"","sources":["../../src/file/FileStorage.abstract.test.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAQ,MAAM,QAAQ,CAAC;AAE3C,eAAO,MAAM,eAAe,gBAAiB,WAAW,SA6FvD,CAAC"}
@@ -44,13 +44,16 @@ const fileStorageTest = (fileStorage) => {
44
44
  });
45
45
  const fileName = `test-${(0, uuid_1.v4)()}.json`;
46
46
  const file = {
47
- key: fileName,
48
- metadata: {},
47
+ reference: {
48
+ _tag: 'FileDescriptor',
49
+ key: fileName,
50
+ metadata: { name: fileName },
51
+ },
49
52
  content: readableStream,
50
53
  };
51
54
  const writtenFile = await fileStorage.write(file, O.none)();
52
55
  expect(writtenFile).toEqual(E.right(file));
53
- const sharedUrl = await fileStorage.getSharedUrl(file.key)();
56
+ const sharedUrl = await fileStorage.getSharedUrl(file.reference.key)();
54
57
  expect(sharedUrl).toEqual(E.right(expect.objectContaining({
55
58
  url: expect.any(String),
56
59
  })));
@@ -59,7 +62,7 @@ const fileStorageTest = (fileStorage) => {
59
62
  expect(sharedUrl.right.expires).toEqual(expect.any(Number));
60
63
  }
61
64
  }
62
- const read = await fileStorage.read(file.key)();
65
+ const read = await fileStorage.read(file.reference.key)();
63
66
  const readResponse = E.getOrElse(() => {
64
67
  throw new Error('Expected right');
65
68
  })(read);
@@ -77,7 +80,7 @@ const fileStorageTest = (fileStorage) => {
77
80
  throw new Error('failed to deserialise stream contents');
78
81
  }
79
82
  expect(deserializedData.right).toStrictEqual(fileContent);
80
- const deleted = await fileStorage.delete(file.key)();
83
+ const deleted = await fileStorage.delete(file.reference.key)();
81
84
  expect(deleted).toEqual(E.right(undefined));
82
85
  });
83
86
  });
@@ -1 +1 @@
1
- {"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAgB,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,qBAAa,iBAAkB,YAAW,WAAW;IACxC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,GAAE,MAAe;IAE7C,KAAK,CACJ,IAAI,EAAE,IAAI,EACV,YAAY,GAAE,CAAC,CAAC,MAAM,CAAC,aAAa,CAAU,GAC5C,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkB7B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAiB7C,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAW1D,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAYpD,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAY7D,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;CAW1D"}
1
+ {"version":3,"file":"NodeFsFileStorage.d.ts","sourceRoot":"","sources":["../../src/file/NodeFsFileStorage.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3C,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EACN,IAAI,EACJ,WAAW,EAEX,SAAS,EAET,MAAM,QAAQ,CAAC;AAEhB,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,qBAAa,iBAAkB,YAAW,WAAW;IACxC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,GAAE,MAAe;IAE7C,KAAK,CACJ,IAAI,EAAE,IAAI,EACV,YAAY,GAAE,CAAC,CAAC,MAAM,CAAC,aAAa,CAAU,GAC5C,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAkB7B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC;IAuB7C,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAW1D,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAYpD,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAY7D,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;CAW1D"}
@@ -42,8 +42,11 @@ describe('NodeFsFileStorage Tests', () => {
42
42
  });
43
43
  const fileName = `test-${(0, uuid_1.v4)()}.txt`;
44
44
  const file = {
45
- key: fileName,
46
- metadata: {},
45
+ reference: {
46
+ _tag: 'FileDescriptor',
47
+ key: fileName,
48
+ metadata: { name: fileName },
49
+ },
47
50
  content: readableStream,
48
51
  };
49
52
  const writtenFile = await fileStorage.write(file, O.none)();
@@ -56,14 +59,17 @@ describe('NodeFsFileStorage Tests', () => {
56
59
  },
57
60
  });
58
61
  const appendFile = {
59
- key: fileName,
60
- metadata: {},
62
+ reference: {
63
+ _tag: 'FileDescriptor',
64
+ key: fileName,
65
+ metadata: { name: fileName },
66
+ },
61
67
  content: appendReadableStream,
62
68
  };
63
69
  const appendedFile = await fileStorage.write(appendFile, O.some({ flag: 'a' }))();
64
70
  expect(appendedFile).toEqual(E.right(appendFile));
65
71
  // When reading the full contents.
66
- const read = await fileStorage.read(file.key)();
72
+ const read = await fileStorage.read(file.reference.key)();
67
73
  const readResponse = E.getOrElse(() => {
68
74
  throw new Error('Expected right');
69
75
  })(read);
@@ -77,7 +83,7 @@ describe('NodeFsFileStorage Tests', () => {
77
83
  });
78
84
  });
79
85
  expect(Buffer.from(data).toString()).toStrictEqual(`${fileContent}${secondFileContent}`);
80
- const deleted = await fileStorage.delete(file.key)();
86
+ const deleted = await fileStorage.delete(file.reference.key)();
81
87
  expect(deleted).toEqual(E.right(undefined));
82
88
  });
83
89
  });
@@ -36,7 +36,7 @@ class NodeFsFileStorage {
36
36
  this.basePath = basePath;
37
37
  }
38
38
  write(file, writeOptions = O.none) {
39
- const filePath = `${this.basePath}/${file.key}`;
39
+ const filePath = `${this.basePath}/${file.reference.key}`;
40
40
  const uploadOptions = O.toNullable(writeOptions);
41
41
  const result = (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => fs.writeFile(filePath, file.content, uploadOptions?.flag ? { flag: uploadOptions.flag } : {}));
42
42
  return (0, function_1.pipe)(result, TE.map(() => file));
@@ -48,9 +48,15 @@ class NodeFsFileStorage {
48
48
  const stat = await fs.stat(filePath);
49
49
  const stream = fileHandle.createReadStream();
50
50
  const metadata = {
51
+ name: key,
51
52
  size: stat.size,
52
53
  };
53
- return { metadata, key: filePath, content: stream };
54
+ const fileReference = {
55
+ _tag: 'FileDescriptor',
56
+ key,
57
+ metadata,
58
+ };
59
+ return { reference: fileReference, content: stream };
54
60
  }, (reason) => new Error(String(reason)));
55
61
  }
56
62
  getSharedUrl(key) {
@@ -1 +1 @@
1
- {"version":3,"file":"Http.d.ts","sourceRoot":"","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AAW,KAAK,aAAa,CAAC;CACzD;AAED,eAAO,MAAM,WAAW,EAAE,oBAyIzB,CAAC;AAEF,eAAO,MAAM,oCAAoC,EAAE,MAAM,CACxD,mBAAmB,EACnB,eAAe,CAIf,CAAC;AAEF,eAAO,MAAM,eAAe,SACrB,eAAe,qCAEkC,CAAC"}
package/dist/http/Http.js CHANGED
@@ -43,10 +43,10 @@ exports.MultipartHttpRequestBody = {
43
43
  return TE.left(Error('Multipart body must be an object'));
44
44
  }
45
45
  const fields = {};
46
- const fileReferences = [];
46
+ const fileDescriptors = [];
47
47
  Object.entries(value).forEach(([key, val]) => {
48
- if (File_1.FileReference.isFileReference(val)) {
49
- fileReferences.push(val);
48
+ if (File_1.FileDescriptor.isFileDescriptor(val)) {
49
+ fileDescriptors.push(val);
50
50
  }
51
51
  else if (!_.isObject(val)) {
52
52
  fields[key] = val;
@@ -55,7 +55,7 @@ exports.MultipartHttpRequestBody = {
55
55
  return TE.left(Error(`Multipart body field '${key}' must be a string, number, boolean or file reference`));
56
56
  }
57
57
  });
58
- return (0, function_1.pipe)(TE.sequenceArray(fileReferences.map((fileReference) => (0, function_1.pipe)(fileStorage.read(fileReference.key), TE.map((file) => ({ key: file.key, file }))))), TE.map((files) => {
58
+ return (0, function_1.pipe)(TE.sequenceArray(fileDescriptors.map((fileReference) => (0, function_1.pipe)(fileStorage.read(fileReference.key), TE.map((file) => ({ key: file.reference.key, file }))))), TE.map((files) => {
59
59
  const filesRecord = {};
60
60
  files.forEach(({ key, file }) => {
61
61
  filesRecord[key] = file;
@@ -208,6 +208,7 @@ exports.HttpHeaders = {
208
208
  };
209
209
  exports.serializationFormatToHttpContentType = {
210
210
  [Serialization_1.SerializationFormat.Json]: HttpContentType.Json,
211
+ [Serialization_1.SerializationFormat.FormUrlEncoded]: HttpContentType.FormUrlEncoded,
211
212
  };
212
213
  const isMultiPartBody = (body) => body?.files !== undefined;
213
214
  exports.isMultiPartBody = isMultiPartBody;
@@ -1 +1 @@
1
- {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAM1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAufpD"}
1
+ {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAO1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAkmBpD"}
@@ -31,6 +31,7 @@ const E = __importStar(require("fp-ts/Either"));
31
31
  const stream_1 = require("stream");
32
32
  const function_1 = require("fp-ts/function");
33
33
  const t = __importStar(require("io-ts"));
34
+ const td = __importStar(require("io-ts-types"));
34
35
  const http_1 = require("http");
35
36
  const util_1 = require("util");
36
37
  const TE = __importStar(require("fp-ts/TaskEither"));
@@ -42,8 +43,10 @@ const Http_1 = require("./Http");
42
43
  const File_1 = require("../file/File");
43
44
  const NodeFsFileStorage_1 = require("../file/NodeFsFileStorage");
44
45
  const BufferExtensions_1 = require("../buffer/BufferExtensions");
46
+ const FormUrlEncodedSerialization_1 = require("../serialization/FormUrlEncodedSerialization");
45
47
  function httpClientTest(httpClient) {
46
48
  const serialization = new JsonSerialization_1.JsonSerialization();
49
+ const formUrlEncodedSerialization = new FormUrlEncodedSerialization_1.FormUrlEncodedSerialization();
47
50
  const fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage();
48
51
  const testErrorResponseCodec = TypeCodec_1.TypeCodec.fromDescriptor(t.type({
49
52
  message: t.string,
@@ -52,12 +55,25 @@ function httpClientTest(httpClient) {
52
55
  strField: t.string,
53
56
  intField: t.number,
54
57
  }));
58
+ const testInputFormUrlEncoded = {
59
+ x: 1,
60
+ arr: [1, 2, 3],
61
+ arr2: [1, [2], 3],
62
+ users: [{ username: 'tray' }],
63
+ };
64
+ const testInputFormUrlEncodedCodec = TypeCodec_1.TypeCodec.fromDescriptor(t.type({
65
+ x: td.NumberFromString,
66
+ arr: t.array(td.NumberFromString),
67
+ arr2: t.array(t.union([td.NumberFromString, t.array(td.NumberFromString)])),
68
+ users: t.array(t.type({
69
+ username: t.string,
70
+ })),
71
+ }));
55
72
  const ReadableCodec = new t.Type('Readable', (input) => input instanceof stream_1.Readable, (input, ctx) => input instanceof stream_1.Readable ? t.success(input) : t.failure(input, ctx), (input) => input);
56
73
  const testMultiPartBodyTypeCodec = TypeCodec_1.TypeCodec.fromDescriptor(t.type({
57
74
  fields: t.record(t.string, t.string),
58
75
  files: t.record(t.string, t.type({
59
- key: t.string,
60
- metadata: File_1.fileMetadataDescriptor,
76
+ reference: File_1.fileDescriptorDescriptor,
61
77
  content: t.any,
62
78
  })),
63
79
  }));
@@ -93,7 +109,10 @@ function httpClientTest(httpClient) {
93
109
  });
94
110
  httpRequest.on('end', () => {
95
111
  const buffer = Buffer.concat(body);
96
- const response = (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encodedRequest) => requestCodec.decode(encodedRequest)), E.map(process), E.map((result) => responseCodec.encode(result)), E.map((encodedResult) => serialization.serialize(encodedResult)), E.getOrElse(() => (new util_1.TextEncoder().encode('Error serializing value').buffer)));
112
+ const requestSerializer = httpRequest.headers['content-type'] === Http_1.HttpContentType.FormUrlEncoded
113
+ ? formUrlEncodedSerialization
114
+ : serialization;
115
+ const response = (0, function_1.pipe)(requestSerializer.deserialize(buffer), E.chain((encodedRequest) => requestCodec.decode(encodedRequest)), E.map(process), E.map((result) => responseCodec.encode(result)), E.map((encodedResult) => serialization.serialize(encodedResult)), E.getOrElse((error) => new util_1.TextEncoder().encode(error.message).buffer));
97
116
  httpResponse.end(Buffer.from(response));
98
117
  });
99
118
  }
@@ -128,9 +147,13 @@ function httpClientTest(httpClient) {
128
147
  return flattenedFiles;
129
148
  };
130
149
  const convertFormidableFileToTrayFile = (file) => ({
131
- key: file.newFilename,
132
- metadata: {
133
- size: file.size,
150
+ reference: {
151
+ _tag: 'FileDescriptor',
152
+ key: file.newFilename,
153
+ metadata: {
154
+ name: file.newFilename,
155
+ size: file.size,
156
+ },
134
157
  },
135
158
  content: stream_1.Readable.from(file.filepath),
136
159
  });
@@ -159,6 +182,14 @@ function httpClientTest(httpClient) {
159
182
  res.writeHead(200, { [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.Json });
160
183
  await processHttpRequest(testMultiPartBodyTypeCodec, testMultiPartBodyTypeCodec, req, res, (input) => input, true);
161
184
  }
185
+ else if (req.method === 'POST' &&
186
+ req.headers['content-type'] === Http_1.HttpContentType.FormUrlEncoded) {
187
+ res.writeHead(200, { [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.Json });
188
+ await processHttpRequest(testInputFormUrlEncodedCodec, testResponseCodec, req, res, (input) => ({
189
+ strField: 'customVal',
190
+ intField: input.arr2[0],
191
+ }));
192
+ }
162
193
  else {
163
194
  const customHeader = req.headers['x-custom-header'];
164
195
  res.writeHead(200, { [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.Json });
@@ -212,6 +243,7 @@ function httpClientTest(httpClient) {
212
243
  const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputCodec.encode(testInput))), TE.map((encoded) => serialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, {
213
244
  headers: {
214
245
  'x-custom-header': 'customVal',
246
+ [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.Json,
215
247
  },
216
248
  pathParams: {},
217
249
  queryString: {},
@@ -229,13 +261,19 @@ function httpClientTest(httpClient) {
229
261
  await fileStorage.delete('file1')();
230
262
  await fileStorage.delete('file2')();
231
263
  const file1 = await fileStorage.write({
232
- key: 'file1',
233
- metadata: {},
264
+ reference: {
265
+ _tag: 'FileDescriptor',
266
+ key: 'file1',
267
+ metadata: { name: 'file1' },
268
+ },
234
269
  content: stream_1.Readable.from('file 1 content'),
235
270
  })();
236
271
  const file2 = await fileStorage.write({
237
- key: 'file2',
238
- metadata: {},
272
+ reference: {
273
+ _tag: 'FileDescriptor',
274
+ key: 'file2',
275
+ metadata: { name: 'file2' },
276
+ },
239
277
  content: stream_1.Readable.from('file 2 content'),
240
278
  })();
241
279
  if (file1._tag === 'Left') {
@@ -251,13 +289,11 @@ function httpClientTest(httpClient) {
251
289
  },
252
290
  files: {
253
291
  file1: {
254
- key: 'file1',
255
- metadata: {},
292
+ reference: file1.right.reference,
256
293
  content: stream_1.Readable.from('Hello world'),
257
294
  },
258
295
  file2: {
259
- key: 'file2',
260
- metadata: {},
296
+ reference: file2.right.reference,
261
297
  content: stream_1.Readable.from('Hello world'),
262
298
  },
263
299
  },
@@ -277,16 +313,39 @@ function httpClientTest(httpClient) {
277
313
  fields: testInput.fields,
278
314
  files: expect.objectContaining({
279
315
  file1: expect.objectContaining({
280
- key: expect.any(String),
281
- metadata: expect.objectContaining({}),
316
+ reference: expect.objectContaining({
317
+ _tag: 'FileDescriptor',
318
+ key: expect.any(String),
319
+ metadata: expect.objectContaining({}),
320
+ }),
282
321
  }),
283
322
  file2: expect.objectContaining({
284
- key: expect.any(String),
285
- metadata: expect.objectContaining({}),
323
+ reference: expect.objectContaining({
324
+ _tag: 'FileDescriptor',
325
+ key: expect.any(String),
326
+ metadata: expect.objectContaining({}),
327
+ }),
286
328
  }),
287
329
  }),
288
330
  })));
289
331
  });
332
+ test('should send a POST request with a formUrlEncoded body', async () => {
333
+ const response = await (0, function_1.pipe)((0, Task_1.createTaskEitherFromEither)(E.right(testInputFormUrlEncodedCodec.encode(testInputFormUrlEncoded))), TE.map((encoded) => formUrlEncodedSerialization.serialize(encoded)), TE.chain((serializedInput) => httpClient.execute(Http_1.HttpMethod.Post, baseUrl, {
334
+ headers: {
335
+ [Http_1.HttpHeader.ContentType]: Http_1.HttpContentType.FormUrlEncoded,
336
+ },
337
+ pathParams: {},
338
+ queryString: {},
339
+ body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(serializedInput),
340
+ })))();
341
+ const testValue = {
342
+ strField: 'customVal',
343
+ intField: 1,
344
+ };
345
+ const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testResponseCodec.decode(encoded)))));
346
+ const resultE = await result();
347
+ expect(resultE).toStrictEqual(E.right(testValue));
348
+ });
290
349
  test('should parse an internal error response', async () => {
291
350
  const response = await httpClient.execute(Http_1.HttpMethod.Get, baseUrl, {
292
351
  headers: { error: '500' },
@@ -0,0 +1,10 @@
1
+ import * as E from 'fp-ts/Either';
2
+ import { Serialization, SerializationFormat } from './Serialization';
3
+ export declare class FormUrlEncodedSerialization implements Serialization {
4
+ private textEncoder;
5
+ private textDecoder;
6
+ format: SerializationFormat;
7
+ serialize(value: unknown): ArrayBuffer;
8
+ deserialize(value: ArrayBuffer): E.Either<Error, unknown>;
9
+ }
10
+ //# sourceMappingURL=FormUrlEncodedSerialization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FormUrlEncodedSerialization.d.ts","sourceRoot":"","sources":["../../src/serialization/FormUrlEncodedSerialization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAIlC,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGrE,qBAAa,2BAA4B,YAAW,aAAa;IAChE,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,WAAW,CAAqB;IAExC,MAAM,EAAE,mBAAmB,CAAsC;IAEjE,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW;IAKtC,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC;CASzD"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.FormUrlEncodedSerialization = void 0;
30
+ const E = __importStar(require("fp-ts/Either"));
31
+ const util_1 = require("util");
32
+ const function_1 = require("fp-ts/function");
33
+ const qs_1 = __importDefault(require("qs"));
34
+ const Serialization_1 = require("./Serialization");
35
+ const Try_1 = require("../try/Try");
36
+ class FormUrlEncodedSerialization {
37
+ textEncoder = new util_1.TextEncoder();
38
+ textDecoder = new util_1.TextDecoder();
39
+ format = Serialization_1.SerializationFormat.FormUrlEncoded;
40
+ serialize(value) {
41
+ const formEncoded = qs_1.default.stringify(value);
42
+ return this.textEncoder.encode(formEncoded).buffer;
43
+ }
44
+ deserialize(value) {
45
+ if (value.byteLength === 0) {
46
+ return E.right(undefined);
47
+ }
48
+ return (0, function_1.pipe)((0, Try_1.tryToEither)(() => this.textDecoder.decode(value)), E.chain((str) => (0, Try_1.tryToEither)(() => qs_1.default.parse(str))));
49
+ }
50
+ }
51
+ exports.FormUrlEncodedSerialization = FormUrlEncodedSerialization;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=FormUrlEncodedSerialization.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FormUrlEncodedSerialization.unit.test.d.ts","sourceRoot":"","sources":["../../src/serialization/FormUrlEncodedSerialization.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Serialization_abstract_test_1 = require("./Serialization.abstract.test");
4
+ const FormUrlEncodedSerialization_1 = require("./FormUrlEncodedSerialization");
5
+ describe('FormUrlEncoded', () => {
6
+ (0, Serialization_abstract_test_1.testSerialization)(new FormUrlEncodedSerialization_1.FormUrlEncodedSerialization());
7
+ });
@@ -32,7 +32,7 @@ function testSerialization(serialization) {
32
32
  test('should serialize and deserialize a value', () => {
33
33
  const testValue = {
34
34
  strField: 'str',
35
- intField: O.some(123),
35
+ intStrField: O.some('123'),
36
36
  };
37
37
  const result = (0, function_1.pipe)(E.right(serialization.serialize(testValue)), E.chain((serialized) => serialization.deserialize(serialized)));
38
38
  expect(result).toStrictEqual(E.right(testValue));
@@ -44,8 +44,8 @@ function testSerialization(serialization) {
44
44
  expect(result).toStrictEqual(expected);
45
45
  });
46
46
  test('should deserialize undefined', () => {
47
- const undefinedSerialised = new ArrayBuffer(0);
48
- const result = serialization.deserialize(undefinedSerialised);
47
+ const undefinedSerialized = new ArrayBuffer(0);
48
+ const result = serialization.deserialize(undefinedSerialized);
49
49
  const expected = E.right(undefined);
50
50
  expect(result).toStrictEqual(expected);
51
51
  });
@@ -1,6 +1,7 @@
1
1
  import * as E from 'fp-ts/Either';
2
2
  export declare enum SerializationFormat {
3
- Json = "Json"
3
+ Json = "Json",
4
+ FormUrlEncoded = "FormUrlEncoded"
4
5
  }
5
6
  export interface Serialization {
6
7
  format: SerializationFormat;
@@ -1 +1 @@
1
- {"version":3,"file":"Serialization.d.ts","sourceRoot":"","sources":["../../src/serialization/Serialization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,oBAAY,mBAAmB;IAC9B,IAAI,SAAS;CACb;AAED,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,mBAAmB,CAAC;IAE5B,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC;IAEvC,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;CAC1D"}
1
+ {"version":3,"file":"Serialization.d.ts","sourceRoot":"","sources":["../../src/serialization/Serialization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,oBAAY,mBAAmB;IAC9B,IAAI,SAAS;IACb,cAAc,mBAAmB;CACjC;AAED,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,mBAAmB,CAAC;IAE5B,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC;IAEvC,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;CAC1D"}
@@ -4,4 +4,5 @@ exports.SerializationFormat = void 0;
4
4
  var SerializationFormat;
5
5
  (function (SerializationFormat) {
6
6
  SerializationFormat["Json"] = "Json";
7
+ SerializationFormat["FormUrlEncoded"] = "FormUrlEncoded";
7
8
  })(SerializationFormat || (exports.SerializationFormat = SerializationFormat = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.47.0",
3
+ "version": "4.49.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"