rads-db 0.1.51 → 0.1.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -219,6 +219,9 @@ interface RestFileUploadDriverOptions {
219
219
  headers?: any;
220
220
  method?: string;
221
221
  }) => any;
222
+ /** If true, "multipart/form-data" body will be used (sometimes causes problems in serverless environments).
223
+ * Otherwise, "application/json" body (with base64-encoded binary content) will be used */
224
+ useFormData?: boolean;
222
225
  }
223
226
  type GenerateClientOptions = Partial<GenerateClientNormalizedOptions>;
224
227
  interface GenerateClientNormalizedOptions {
@@ -229,7 +232,7 @@ interface RadsRequestContext {
229
232
  getUser?: () => {
230
233
  id: string;
231
234
  role: string;
232
- };
235
+ } | undefined;
233
236
  }
234
237
 
235
238
  declare function entity<T>(meta?: EntityDecoratorArgs): (classConstructor: new () => T, _ctx?: ClassDecoratorContext<any>) => void;
@@ -8,31 +8,48 @@ var _lodash = _interopRequireDefault(require("lodash"));
8
8
  var _pluralize = _interopRequireDefault(require("pluralize"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
10
  var _default = options => (schema, entity) => {
11
- options = {
11
+ const opts = {
12
12
  baseUrl: "/api",
13
13
  fetch: globalThis.fetch,
14
+ getHeaders: () => {
15
+ return void 0;
16
+ },
14
17
  ...options
15
18
  };
16
- const fetch = options.fetch || global.fetch;
17
- if (!options.fetch) throw new Error('Please provide "fetch" argument to rest driver definition');
19
+ const fetch = opts.fetch || global.fetch;
20
+ if (!opts.fetch) throw new Error('Please provide "fetch" argument to rest driver definition');
18
21
  const pluralEntityName = _lodash.default.lowerFirst((0, _pluralize.default)(entity));
19
22
  const instance = {
20
23
  driverName: "restApi",
21
24
  async getMany(args) {
22
25
  args = args || {};
23
- const response = await fetch(`${options.baseUrl}/${pluralEntityName}`, {
26
+ const url = `${opts.baseUrl}/${pluralEntityName}`;
27
+ const fetchOptions = {
24
28
  method: "POST",
25
- body: JSON.stringify(args)
26
- });
29
+ body: JSON.stringify(args),
30
+ headers: {}
31
+ };
32
+ fetchOptions.headers = {
33
+ ...fetchOptions.headers,
34
+ ...opts.getHeaders(url, fetchOptions)
35
+ };
36
+ const response = await fetch(url, fetchOptions);
27
37
  return await response?.json();
28
38
  },
29
39
  async putMany(item) {
30
- await fetch(`${options.baseUrl}/${pluralEntityName}`, {
40
+ const url = `${opts.baseUrl}/${pluralEntityName}`;
41
+ const fetchOptions = {
31
42
  method: "PUT",
32
43
  body: JSON.stringify({
33
44
  data: item
34
- })
35
- });
45
+ }),
46
+ headers: {}
47
+ };
48
+ fetchOptions.headers = {
49
+ ...fetchOptions.headers,
50
+ ...opts.getHeaders(url, fetchOptions)
51
+ };
52
+ await fetch(url, fetchOptions);
36
53
  }
37
54
  };
38
55
  return instance;
@@ -6,6 +6,7 @@ interface RestDriverOptions {
6
6
  headers?: any;
7
7
  method?: string;
8
8
  }) => any;
9
+ getHeaders?: (url: string, fetchOptions: RequestInit) => Record<string, string> | undefined;
9
10
  }
10
11
  declare const _default: (options: RestDriverOptions) => (schema: Schema, entity: string) => MinimalDriver;
11
12
  export default _default;
@@ -1,26 +1,41 @@
1
1
  import _ from "lodash";
2
2
  import pluralize from "pluralize";
3
3
  export default (options) => (schema, entity) => {
4
- options = { baseUrl: "/api", fetch: globalThis.fetch, ...options };
5
- const fetch = options.fetch || global.fetch;
6
- if (!options.fetch)
4
+ const opts = {
5
+ baseUrl: "/api",
6
+ fetch: globalThis.fetch,
7
+ getHeaders: () => {
8
+ return void 0;
9
+ },
10
+ ...options
11
+ };
12
+ const fetch = opts.fetch || global.fetch;
13
+ if (!opts.fetch)
7
14
  throw new Error('Please provide "fetch" argument to rest driver definition');
8
15
  const pluralEntityName = _.lowerFirst(pluralize(entity));
9
16
  const instance = {
10
17
  driverName: "restApi",
11
18
  async getMany(args) {
12
19
  args = args || {};
13
- const response = await fetch(`${options.baseUrl}/${pluralEntityName}`, {
20
+ const url = `${opts.baseUrl}/${pluralEntityName}`;
21
+ const fetchOptions = {
14
22
  method: "POST",
15
- body: JSON.stringify(args)
16
- });
23
+ body: JSON.stringify(args),
24
+ headers: {}
25
+ };
26
+ fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
27
+ const response = await fetch(url, fetchOptions);
17
28
  return await response?.json();
18
29
  },
19
30
  async putMany(item) {
20
- await fetch(`${options.baseUrl}/${pluralEntityName}`, {
31
+ const url = `${opts.baseUrl}/${pluralEntityName}`;
32
+ const fetchOptions = {
21
33
  method: "PUT",
22
- body: JSON.stringify({ data: item })
23
- });
34
+ body: JSON.stringify({ data: item }),
35
+ headers: {}
36
+ };
37
+ fetchOptions.headers = { ...fetchOptions.headers, ...opts.getHeaders(url, fetchOptions) };
38
+ await fetch(url, fetchOptions);
24
39
  }
25
40
  };
26
41
  return instance;
@@ -15,16 +15,32 @@ var _default = options => {
15
15
  const driver = {
16
16
  driverName: "restApi",
17
17
  async uploadFile(args) {
18
- const form = new FormData();
19
- form.append("containerName", args.containerName || "");
20
- form.append("blob", args.blob, args.fileName);
18
+ const body = options.useFormData ? getFormDataBody(args) : await getJsonBody(args);
21
19
  const response = await fetch(`${options.baseUrl}/uploadFile`, {
22
20
  method: "POST",
23
- body: form
21
+ body
24
22
  });
25
23
  return response.text();
26
24
  }
27
25
  };
28
26
  return driver;
29
27
  };
30
- module.exports = _default;
28
+ module.exports = _default;
29
+ function getFormDataBody(args) {
30
+ const form = new FormData();
31
+ form.append("containerName", args.containerName || "");
32
+ form.append("blob", args.blob, args.fileName);
33
+ return form;
34
+ }
35
+ async function getJsonBody(args) {
36
+ return JSON.stringify({
37
+ containerName: args.containerName,
38
+ fileName: args.fileName,
39
+ blobDataUrl: await blobToDataUrl(args.blob)
40
+ });
41
+ }
42
+ async function blobToDataUrl(blob) {
43
+ const buf = await blob.arrayBuffer();
44
+ const b64string = Buffer.from(buf).toString("base64");
45
+ return `data:${blob.type};base64,${b64string}`;
46
+ }
@@ -6,15 +6,31 @@ export default (options) => {
6
6
  const driver = {
7
7
  driverName: "restApi",
8
8
  async uploadFile(args) {
9
- const form = new FormData();
10
- form.append("containerName", args.containerName || "");
11
- form.append("blob", args.blob, args.fileName);
9
+ const body = options.useFormData ? getFormDataBody(args) : await getJsonBody(args);
12
10
  const response = await fetch(`${options.baseUrl}/uploadFile`, {
13
11
  method: "POST",
14
- body: form
12
+ body
15
13
  });
16
14
  return response.text();
17
15
  }
18
16
  };
19
17
  return driver;
20
18
  };
19
+ function getFormDataBody(args) {
20
+ const form = new FormData();
21
+ form.append("containerName", args.containerName || "");
22
+ form.append("blob", args.blob, args.fileName);
23
+ return form;
24
+ }
25
+ async function getJsonBody(args) {
26
+ return JSON.stringify({
27
+ containerName: args.containerName,
28
+ fileName: args.fileName,
29
+ blobDataUrl: await blobToDataUrl(args.blob)
30
+ });
31
+ }
32
+ async function blobToDataUrl(blob) {
33
+ const buf = await blob.arrayBuffer();
34
+ const b64string = Buffer.from(buf).toString("base64");
35
+ return `data:${blob.type};base64,${b64string}`;
36
+ }
package/package.json CHANGED
@@ -34,7 +34,7 @@
34
34
  "require": "./integrations/*.cjs"
35
35
  }
36
36
  },
37
- "version": "0.1.51",
37
+ "version": "0.1.53",
38
38
  "description": "Say goodbye to boilerplate code and hello to efficient and elegant syntax.",
39
39
  "keywords": [],
40
40
  "author": "",