@dfns/sdk 0.8.18 → 0.8.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfns/sdk",
3
- "version": "0.8.18",
3
+ "version": "0.8.20",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/dfns/dfns-sdk-ts.git",
package/utils/fetch.d.ts CHANGED
@@ -4,11 +4,16 @@ export type FetchOptions<T> = {
4
4
  method: HttpMethod;
5
5
  headers?: Record<string, string>;
6
6
  body?: string | unknown;
7
+ file?: {
8
+ bytes: Uint8Array;
9
+ name?: string;
10
+ };
7
11
  apiOptions: T;
8
12
  };
9
13
  export type Fetch<T> = (resource: string | URL, options: FetchOptions<T>) => Promise<Response>;
10
14
  export declare const fullUrl: <T extends DfnsBaseApiOptions>(fetch: Fetch<T>) => Fetch<T>;
11
15
  export declare const jsonSerializer: <T>(fetch: Fetch<T>) => Fetch<T>;
16
+ export declare const formDataSerializer: <T>(fetch: Fetch<T>) => Fetch<T>;
12
17
  export declare const errorHandler: <T>(fetch: Fetch<T>) => Fetch<T>;
13
18
  export declare const catchPolicyPending: <T>(fetch: Fetch<T>) => Fetch<T>;
14
19
  export declare const dfnsAuth: <T extends DfnsBaseApiOptions>(fetch: Fetch<T>) => Fetch<T>;
package/utils/fetch.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.simpleFetch = exports.dfnsAuth = exports.catchPolicyPending = exports.errorHandler = exports.jsonSerializer = exports.fullUrl = void 0;
3
+ exports.simpleFetch = exports.dfnsAuth = exports.catchPolicyPending = exports.errorHandler = exports.formDataSerializer = exports.jsonSerializer = exports.fullUrl = void 0;
4
4
  const cross_fetch_1 = require("cross-fetch");
5
5
  const dfnsError_1 = require("../dfnsError");
6
+ const authToken_1 = require("./authToken");
7
+ const sha256_1 = require("./sha256");
6
8
  const DEFAULT_DFNS_BASE_URL = 'https://api.dfns.io';
7
9
  const package_json_1 = require("../package.json");
8
- const authToken_1 = require("./authToken");
9
10
  const fullUrl = (fetch) => {
10
11
  return async (resource, options) => {
11
12
  const baseUrl = options.apiOptions.baseUrl || DEFAULT_DFNS_BASE_URL;
@@ -16,7 +17,7 @@ const fullUrl = (fetch) => {
16
17
  exports.fullUrl = fullUrl;
17
18
  const jsonSerializer = (fetch) => {
18
19
  return async (resource, options) => {
19
- if (options.body) {
20
+ if (options.body && !(options.body instanceof FormData)) {
20
21
  options.body = JSON.stringify(options.body);
21
22
  options.headers = {
22
23
  'content-type': 'application/json',
@@ -27,6 +28,20 @@ const jsonSerializer = (fetch) => {
27
28
  };
28
29
  };
29
30
  exports.jsonSerializer = jsonSerializer;
31
+ const formDataSerializer = (fetch) => {
32
+ return async (resource, options) => {
33
+ if (!options.file)
34
+ return fetch(resource, options);
35
+ const { bytes, name } = options.file;
36
+ const fileChecksum = await (0, sha256_1.sha256)(bytes);
37
+ const body = { ...(options.body ?? {}), fileChecksum };
38
+ const form = new FormData();
39
+ form.append('data', JSON.stringify(body));
40
+ form.append('file', new Blob([bytes]), name);
41
+ return fetch(resource, { ...options, body: form });
42
+ };
43
+ };
44
+ exports.formDataSerializer = formDataSerializer;
30
45
  const errorHandler = (fetch) => {
31
46
  return async (resource, options) => {
32
47
  const response = await fetch(resource, options);
@@ -0,0 +1 @@
1
+ export declare const sha256: (bytes: Uint8Array) => Promise<string>;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sha256 = void 0;
4
+ const string_1 = require("./string");
5
+ const sha256 = async (bytes) => {
6
+ const digest = await crypto.subtle.digest('SHA-256', bytes);
7
+ return (0, string_1.toHex)(digest);
8
+ };
9
+ exports.sha256 = sha256;
@@ -19,8 +19,14 @@ const userAction = (fetch) => {
19
19
  ` the user action challenge, using your credential.`,
20
20
  });
21
21
  }
22
+ const body = (options.body instanceof FormData ? options.body.get('data') : options.body) ?? '';
23
+ if (typeof body !== 'string') {
24
+ throw new dfnsError_1.DfnsError(-1, 'unexpected fetch body for user action signing', {
25
+ details: { type: typeof body },
26
+ });
27
+ }
22
28
  const challenge = await baseAuthApi_1.BaseAuthApi.createUserActionChallenge({
23
- userActionPayload: options.body ?? '',
29
+ userActionPayload: body,
24
30
  userActionHttpMethod: options.method,
25
31
  userActionHttpPath: resource.pathname,
26
32
  userActionServerKind: apiOptions?.userActionServerKind || 'Api',
@@ -38,4 +44,4 @@ const userAction = (fetch) => {
38
44
  return fetch(resource, options);
39
45
  };
40
46
  };
41
- exports.userActionFetch = (0, fetch_1.fullUrl)((0, fetch_1.jsonSerializer)((0, fetch_1.dfnsAuth)(userAction((0, fetch_1.catchPolicyPending)((0, fetch_1.errorHandler)(cross_fetch_1.fetch))))));
47
+ exports.userActionFetch = (0, fetch_1.fullUrl)((0, fetch_1.formDataSerializer)((0, fetch_1.jsonSerializer)((0, fetch_1.dfnsAuth)(userAction((0, fetch_1.catchPolicyPending)((0, fetch_1.errorHandler)(cross_fetch_1.fetch)))))));