@cloudbase/oauth 0.1.1-alpha.6 → 0.1.1-alpha.8

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 (63) hide show
  1. package/.prettierrc.js +41 -0
  2. package/dist/app/index.d.ts +8 -7
  3. package/dist/app/index.js +1 -4
  4. package/dist/app/index.js.map +1 -1
  5. package/dist/app/openuri.d.ts +1 -1
  6. package/dist/app/openuri.js +18 -15
  7. package/dist/app/openuri.js.map +1 -1
  8. package/dist/app/request.d.ts +1 -1
  9. package/dist/app/request.js +8 -2
  10. package/dist/app/request.js.map +1 -1
  11. package/dist/auth/index.d.ts +1 -1
  12. package/dist/oauthclient/index.d.ts +4 -4
  13. package/dist/oauthclient/interface.d.ts +1 -1
  14. package/dist/oauthclient/oauthclient.js +1 -1
  15. package/dist/oauthclient/oauthclient.js.map +1 -1
  16. package/dist/package.json +1 -1
  17. package/package.json +1 -1
  18. package/packages/auth/dist/cjs/index.d.ts +15 -1
  19. package/packages/auth/dist/cjs/index.js +22 -3
  20. package/packages/auth/dist/esm/index.d.ts +15 -1
  21. package/packages/auth/dist/esm/index.js +23 -4
  22. package/packages/container/dist/cjs/index.d.ts +9 -0
  23. package/packages/container/dist/cjs/index.js +70 -155
  24. package/packages/container/dist/cjs/utils/index.d.ts +1 -1
  25. package/packages/container/dist/cjs/utils/index.js +31 -13
  26. package/packages/container/dist/cjs/utils/loader.d.ts +8 -0
  27. package/packages/container/dist/cjs/utils/loader.js +186 -0
  28. package/packages/container/dist/esm/index.d.ts +9 -0
  29. package/packages/container/dist/esm/index.js +69 -151
  30. package/packages/container/dist/esm/utils/index.d.ts +1 -1
  31. package/packages/container/dist/esm/utils/index.js +31 -13
  32. package/packages/container/dist/esm/utils/loader.d.ts +8 -0
  33. package/packages/container/dist/esm/utils/loader.js +178 -0
  34. package/packages/container/dist/miniprogram/index.js +1 -0
  35. package/packages/oauth/dist/cjs/auth/apis.d.ts +17 -0
  36. package/packages/oauth/dist/cjs/auth/apis.js +27 -8
  37. package/packages/oauth/dist/cjs/auth/consts.d.ts +3 -0
  38. package/packages/oauth/dist/cjs/auth/consts.js +4 -1
  39. package/packages/oauth/dist/cjs/captcha/captcha.js +52 -28
  40. package/packages/oauth/dist/cjs/index.js +3 -3
  41. package/packages/oauth/dist/cjs/oauth2client/consts.js +1 -1
  42. package/packages/oauth/dist/cjs/utils/encrypt.js +1 -1
  43. package/packages/oauth/dist/cjs/utils/encryptlong/index.d.ts +1 -1
  44. package/packages/oauth/dist/cjs/utils/encryptlong/index.js +2 -2
  45. package/packages/oauth/dist/cjs/utils/mp.d.ts +1 -0
  46. package/packages/oauth/dist/cjs/utils/mp.js +41 -0
  47. package/packages/oauth/dist/esm/auth/apis.d.ts +17 -0
  48. package/packages/oauth/dist/esm/auth/apis.js +27 -8
  49. package/packages/oauth/dist/esm/auth/consts.d.ts +3 -0
  50. package/packages/oauth/dist/esm/auth/consts.js +4 -1
  51. package/packages/oauth/dist/esm/captcha/captcha.js +52 -28
  52. package/packages/oauth/dist/esm/index.js +3 -3
  53. package/packages/oauth/dist/esm/oauth2client/consts.js +1 -1
  54. package/packages/oauth/dist/esm/utils/encrypt.js +1 -1
  55. package/packages/oauth/dist/esm/utils/encryptlong/index.d.ts +1 -1
  56. package/packages/oauth/dist/esm/utils/encryptlong/index.js +2 -2
  57. package/packages/oauth/dist/esm/utils/mp.d.ts +1 -0
  58. package/packages/oauth/dist/esm/utils/mp.js +37 -0
  59. package/packages/src/dist/oauth/src/index.d.ts +1 -0
  60. package/packages/src/dist/src/index.d.ts +9 -0
  61. package/src/app/index.ts +56 -65
  62. package/src/app/openuri.ts +99 -92
  63. package/src/app/request.ts +52 -50
@@ -0,0 +1 @@
1
+ export declare function isMp(): boolean;
@@ -0,0 +1,37 @@
1
+ export function isMp() {
2
+ var wx = globalThis.wx;
3
+ if (typeof wx === 'undefined') {
4
+ return false;
5
+ }
6
+ if (globalThis.Page === undefined) {
7
+ return false;
8
+ }
9
+ if (!wx.getSystemInfoSync) {
10
+ return false;
11
+ }
12
+ if (!wx.getStorageSync) {
13
+ return false;
14
+ }
15
+ if (!wx.setStorageSync) {
16
+ return false;
17
+ }
18
+ if (!wx.connectSocket) {
19
+ return false;
20
+ }
21
+ if (!wx.request) {
22
+ return false;
23
+ }
24
+ try {
25
+ if (!wx.getSystemInfoSync()) {
26
+ return false;
27
+ }
28
+ if (wx.getSystemInfoSync().AppPlatform === 'qq') {
29
+ return false;
30
+ }
31
+ }
32
+ catch (e) {
33
+ return false;
34
+ }
35
+ return true;
36
+ }
37
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibXAuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdXRpbHMvbXAudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsTUFBTSxVQUFVLElBQUk7SUFDVixJQUFBLEVBQUUsR0FBSyxVQUFpQixHQUF0QixDQUFzQjtJQUNoQyxJQUFJLE9BQU8sRUFBRSxLQUFLLFdBQVcsRUFBRTtRQUM3QixPQUFPLEtBQUssQ0FBQTtLQUNiO0lBQ0QsSUFBSyxVQUFrQixDQUFDLElBQUksS0FBSyxTQUFTLEVBQUU7UUFDMUMsT0FBTyxLQUFLLENBQUE7S0FDYjtJQUNELElBQUksQ0FBQyxFQUFFLENBQUMsaUJBQWlCLEVBQUU7UUFDekIsT0FBTyxLQUFLLENBQUE7S0FDYjtJQUNELElBQUksQ0FBQyxFQUFFLENBQUMsY0FBYyxFQUFFO1FBQ3RCLE9BQU8sS0FBSyxDQUFBO0tBQ2I7SUFDRCxJQUFJLENBQUMsRUFBRSxDQUFDLGNBQWMsRUFBRTtRQUN0QixPQUFPLEtBQUssQ0FBQTtLQUNiO0lBQ0QsSUFBSSxDQUFDLEVBQUUsQ0FBQyxhQUFhLEVBQUU7UUFDckIsT0FBTyxLQUFLLENBQUE7S0FDYjtJQUNELElBQUksQ0FBQyxFQUFFLENBQUMsT0FBTyxFQUFFO1FBQ2YsT0FBTyxLQUFLLENBQUE7S0FDYjtJQUVELElBQUk7UUFDRixJQUFJLENBQUMsRUFBRSxDQUFDLGlCQUFpQixFQUFFLEVBQUU7WUFDM0IsT0FBTyxLQUFLLENBQUE7U0FDYjtRQUVELElBQUksRUFBRSxDQUFDLGlCQUFpQixFQUFFLENBQUMsV0FBVyxLQUFLLElBQUksRUFBRTtZQUMvQyxPQUFPLEtBQUssQ0FBQTtTQUNiO0tBQ0Y7SUFBQyxPQUFPLENBQUMsRUFBRTtRQUNWLE9BQU8sS0FBSyxDQUFBO0tBQ2I7SUFFRCxPQUFPLElBQUksQ0FBQTtBQUNiLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZnVuY3Rpb24gaXNNcCgpIHtcbiAgY29uc3QgeyB3eCB9ID0gZ2xvYmFsVGhpcyBhcyBhbnlcbiAgaWYgKHR5cGVvZiB3eCA9PT0gJ3VuZGVmaW5lZCcpIHtcbiAgICByZXR1cm4gZmFsc2VcbiAgfVxuICBpZiAoKGdsb2JhbFRoaXMgYXMgYW55KS5QYWdlID09PSB1bmRlZmluZWQpIHtcbiAgICByZXR1cm4gZmFsc2VcbiAgfVxuICBpZiAoIXd4LmdldFN5c3RlbUluZm9TeW5jKSB7XG4gICAgcmV0dXJuIGZhbHNlXG4gIH1cbiAgaWYgKCF3eC5nZXRTdG9yYWdlU3luYykge1xuICAgIHJldHVybiBmYWxzZVxuICB9XG4gIGlmICghd3guc2V0U3RvcmFnZVN5bmMpIHtcbiAgICByZXR1cm4gZmFsc2VcbiAgfVxuICBpZiAoIXd4LmNvbm5lY3RTb2NrZXQpIHtcbiAgICByZXR1cm4gZmFsc2VcbiAgfVxuICBpZiAoIXd4LnJlcXVlc3QpIHtcbiAgICByZXR1cm4gZmFsc2VcbiAgfVxuXG4gIHRyeSB7XG4gICAgaWYgKCF3eC5nZXRTeXN0ZW1JbmZvU3luYygpKSB7XG4gICAgICByZXR1cm4gZmFsc2VcbiAgICB9XG5cbiAgICBpZiAod3guZ2V0U3lzdGVtSW5mb1N5bmMoKS5BcHBQbGF0Zm9ybSA9PT0gJ3FxJykge1xuICAgICAgcmV0dXJuIGZhbHNlXG4gICAgfVxuICB9IGNhdGNoIChlKSB7XG4gICAgcmV0dXJuIGZhbHNlXG4gIH1cblxuICByZXR1cm4gdHJ1ZVxufVxuIl19
@@ -0,0 +1 @@
1
+ export * from '@cloudbase/oauth';
@@ -0,0 +1,9 @@
1
+ import cloudbase from '@cloudbase/app';
2
+ import { ICloudbase } from '@cloudbase/types';
3
+ declare global {
4
+ interface Window {
5
+ cloudbase: ICloudbase;
6
+ }
7
+ }
8
+ export = cloudbase;
9
+ export default cloudbase;
package/src/app/index.ts CHANGED
@@ -1,83 +1,74 @@
1
- import {defaultRequest, RequestFn as requestFn, RequestOptions as requestOptions} from "./request";
2
- import {OpenURIWithCallback as openURIWithCallback, defaultOpenURIWithCallback} from "./openuri";
3
- import {DefaultStorage, Storage as storage} from "./storage";
1
+ import { defaultRequest, RequestFn as requestFn, RequestOptions as requestOptions } from './request';
2
+ import { OpenURIWithCallback as openURIWithCallback, defaultOpenURIWithCallback } from './openuri';
3
+ import { DefaultStorage, Storage as storage } from './storage';
4
4
 
5
5
  export interface Options {
6
- env: string,
7
- region?: string,
8
- apiOrigin?: string;
9
- clientId?: string;
10
- clientSecret?: string;
11
- custom?: any;
6
+ env: string;
7
+ region?: string;
8
+ apiOrigin?: string;
9
+ clientId?: string;
10
+ clientSecret?: string;
11
+ custom?: any;
12
12
 
13
- // plugins can set platform functions
14
- request?: requestFn;
15
- storage?: storage;
16
- openURIWithCallback?: openURIWithCallback;
13
+ // plugins can set platform functions
14
+ request?: requestFn;
15
+ baseRequest?: requestFn;
16
+ storage?: storage;
17
+ openURIWithCallback?: openURIWithCallback;
17
18
  }
18
19
 
19
- export function initializeApp(
20
- options: Options,
21
- ): App {
22
- return new AppImpl(options);
20
+ export function initializeApp(options: Options): App {
21
+ return new AppImpl(options);
23
22
  }
24
23
 
25
24
  export interface App {
26
- readonly options: Options;
25
+ readonly options: Options;
27
26
  }
28
27
 
29
28
  export class AppImpl implements App {
30
- private readonly _options: Options;
31
- private readonly _container = new Map<string, any>();
29
+ private readonly _options: Options;
30
+ private readonly _container = new Map<string, any>();
32
31
 
33
- constructor(options: Options) {
34
- if (!options.region) {
35
- options.region = 'ap-shanghai'
36
- }
37
- if (!options.apiOrigin) {
38
- options.apiOrigin = `https://${options.env}.${options.region}.tcb-api.tencentcloudapi.com`
39
- }
40
- let storageEnv = options.env
41
- if (!options.clientId) {
42
- options.clientId = ''
43
- } else {
44
- storageEnv = options.clientId
45
- }
46
- if (!options.storage) {
47
- options.storage = new DefaultStorage({env: storageEnv});
48
- }
49
- if (!options.openURIWithCallback) {
50
- options.openURIWithCallback = defaultOpenURIWithCallback;
51
- }
52
- let baseRequest = options.request
53
- if (!baseRequest) {
54
- baseRequest = defaultRequest
55
- }
56
- const apiOrigin = options.apiOrigin
57
- options.request = async <T>(
58
- url: string,
59
- options?: RequestOptions,
60
- ): Promise<T> => {
61
- if (url.startsWith('/')) {
62
- url = apiOrigin + url;
63
- }
64
- return baseRequest<T>(url, options)
65
- }
66
- this._options = options;
32
+ constructor(options: Options) {
33
+ if (!options.region) {
34
+ options.region = 'ap-shanghai';
67
35
  }
68
-
69
- get options(): Options {
70
- return this._options;
36
+ if (!options.apiOrigin) {
37
+ options.apiOrigin = `https://${options.env}.${options.region}.tcb-api.tencentcloudapi.com`;
71
38
  }
72
-
73
- get container(): Map<string, any> {
74
- return this._container;
39
+ let storageEnv = options.env;
40
+ if (!options.clientId) {
41
+ options.clientId = '';
42
+ } else {
43
+ storageEnv = options.clientId;
75
44
  }
76
- }
45
+ if (!options.storage) {
46
+ options.storage = new DefaultStorage({ env: storageEnv });
47
+ }
48
+ if (!options.openURIWithCallback) {
49
+ options.openURIWithCallback = defaultOpenURIWithCallback;
50
+ }
51
+ let baseRequest: requestFn = options.baseRequest || options.request || defaultRequest;
52
+ const apiOrigin = options.apiOrigin;
53
+ options.request = async <T>(url: string, options?: RequestOptions): Promise<T> => {
54
+ if (url.startsWith('/')) {
55
+ url = apiOrigin + url;
56
+ }
57
+ return baseRequest<T>(url, options);
58
+ };
59
+ this._options = options;
60
+ }
77
61
 
62
+ get options(): Options {
63
+ return this._options;
64
+ }
78
65
 
79
- export type RequestFn = requestFn
80
- export type RequestOptions = requestOptions
81
- export type OpenURIWithCallback = openURIWithCallback
82
- export type Storage = storage
66
+ get container(): Map<string, any> {
67
+ return this._container;
68
+ }
69
+ }
83
70
 
71
+ export type RequestFn = requestFn;
72
+ export type RequestOptions = requestOptions;
73
+ export type OpenURIWithCallback = openURIWithCallback;
74
+ export type Storage = storage;
@@ -1,8 +1,8 @@
1
1
  export type OpenURIWithCallback = (url: string, opts?: OpenURIOptions) => Promise<{ [key: string]: string }>;
2
2
 
3
3
  export interface OpenURIOptions {
4
- width: string;
5
- height: string;
4
+ width: string;
5
+ height: string;
6
6
  }
7
7
 
8
8
  /**
@@ -17,98 +17,105 @@ export interface OpenURIOptions {
17
17
  }, false);
18
18
  *
19
19
  */
20
- export const defaultOpenURIWithCallback: OpenURIWithCallback = (url: string, opts?: OpenURIOptions): Promise<{ [key: string]: string }> => {
21
- let iframeTag = '__iframe';
22
- if (window.location.search.indexOf(iframeTag) > 0) {
23
- document.body.style.display = 'none';
24
- }
25
- if (!opts) {
26
- opts = {
27
- width: '355px',
28
- height: '355px'
29
- }
30
- }
31
- if (document.getElementById('_iframe_panel_wrap') === null) {
32
- var elementDiv = document.createElement('div');
33
- elementDiv.style.cssText =
34
- 'background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;';
35
- elementDiv.setAttribute('id', '_iframe_panel_wrap');
36
- document.body.appendChild(elementDiv);
37
- }
38
- const target = document.getElementById('_iframe_panel_wrap'),
39
- iframe = document.createElement('iframe')
40
- target.innerHTML = '';
41
- const openURL = new URL(url);
42
- const redirectUri = openURL.searchParams.get('redirect_uri')
43
- if (redirectUri) {
44
- const redirectUrl = new URL(redirectUri);
45
- redirectUrl.searchParams.append(iframeTag, "on")
46
- openURL.searchParams.set('redirect_uri', redirectUrl.href)
47
- url = openURL.href
48
- }
49
- iframe.setAttribute('src', url)
50
- iframe.setAttribute('id', '_iframe_panel_wrap_iframe')
51
- iframe.style.cssText = `min-width:${opts.width};display:block;height:${opts.height};margin:0 auto;background-color: rgb(255, 255, 255);border: none;`;
52
- target.appendChild(iframe);
53
- target.style.display = 'block';
20
+ export const defaultOpenURIWithCallback: OpenURIWithCallback = (
21
+ url: string,
22
+ opts?: OpenURIOptions,
23
+ ): Promise<{ [key: string]: string }> => {
24
+ let iframeTag = '__iframe';
25
+ if (window.location.search.indexOf(iframeTag) > 0) {
26
+ document.body.style.display = 'none';
27
+ }
28
+ if (!opts) {
29
+ opts = {
30
+ width: '355px',
31
+ height: '355px',
32
+ };
33
+ }
34
+ if (document.getElementById('_iframe_panel_wrap') === null) {
35
+ var elementDiv = document.createElement('div');
36
+ elementDiv.style.cssText =
37
+ 'background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;';
38
+ elementDiv.setAttribute('id', '_iframe_panel_wrap');
39
+ document.body.appendChild(elementDiv);
40
+ }
41
+ const target = document.getElementById('_iframe_panel_wrap'),
42
+ iframe = document.createElement('iframe');
43
+ target.innerHTML = '';
44
+ const openURL = new URL(url);
45
+ let search = openURL.search.substring(1);
46
+ let contactSign = '?';
47
+ if (openURL.hash.indexOf('=') > 0) {
48
+ search = openURL.hash.substring(1);
49
+ contactSign = '#';
50
+ }
51
+ const searchParams = new URLSearchParams(search);
52
+ const redirectUri = searchParams.get('redirect_uri');
53
+ if (redirectUri) {
54
+ const redirectUrl = new URL(redirectUri);
55
+ redirectUrl.searchParams.append(iframeTag, 'on');
56
+ searchParams.set('redirect_uri', redirectUrl.href);
57
+ url = openURL.origin + openURL.pathname + contactSign + searchParams.toString();
58
+ }
59
+ iframe.setAttribute('src', url);
60
+ iframe.setAttribute('id', '_iframe_panel_wrap_iframe');
61
+ iframe.style.cssText = `min-width:${opts.width};display:block;height:${opts.height};margin:0 auto;background-color: rgb(255, 255, 255);border: none;`;
62
+ target.appendChild(iframe);
63
+ target.style.display = 'block';
54
64
 
55
- let callBack = new Callback()
56
- // handle callback from iframe post message
57
- window.addEventListener('message', e => {
58
- if (e.origin == openURL.origin && callBack.callFunc) {
59
- if (!(typeof e.data === "string")) {
60
- return;
61
- }
62
- target.style.display = 'none';
63
- const data = JSON.parse(e.data);
64
- try {
65
- callBack.callFunc(data)
66
- } catch (e) {
67
- }
65
+ let callBack = new Callback();
66
+ // handle callback from iframe post message
67
+ window.addEventListener(
68
+ 'message',
69
+ (e) => {
70
+ if (e.origin == openURL.origin && callBack.callFunc) {
71
+ if (!(typeof e.data === 'string')) {
72
+ return;
68
73
  }
69
- }, false);
70
- return new Promise<{ [key: string]: string }>((resolve, reject) => {
71
- callBack.callFunc = data => {
72
- if (data.error) {
73
- return reject(data)
74
- }
75
- return resolve(data)
74
+ target.style.display = 'none';
75
+ const data = JSON.parse(e.data);
76
+ try {
77
+ callBack.callFunc(data);
78
+ } catch (e) {}
79
+ }
80
+ },
81
+ false,
82
+ );
83
+ return new Promise<{ [key: string]: string }>((resolve, reject) => {
84
+ callBack.callFunc = (data) => {
85
+ if (data.error) {
86
+ return reject(data);
87
+ }
88
+ return resolve(data);
89
+ };
90
+ // handle callback from iframe redirect uri
91
+ iframe.onload = () => {
92
+ try {
93
+ var windowLocation = window.location;
94
+ var iframeLocation = iframe.contentWindow.location;
95
+ if (iframeLocation.host + iframeLocation.pathname === windowLocation.host + windowLocation.pathname) {
96
+ target.style.display = 'none';
97
+ const iframeUrlParams = new URLSearchParams(iframeLocation.search);
98
+ const data: { [key: string]: string } = {};
99
+ iframeUrlParams.forEach((v, k) => {
100
+ data[k] = v;
101
+ });
102
+ if (data.error) {
103
+ return reject({
104
+ error: iframeUrlParams.get('error'),
105
+ error_description: iframeUrlParams.get('error_description'),
106
+ });
107
+ }
108
+ return resolve(data);
109
+ } else {
110
+ target.style.display = 'block';
76
111
  }
77
- // handle callback from iframe redirect uri
78
- iframe.onload = () => {
79
- try {
80
- var windowLocation = window.location;
81
- var iframeLocation = iframe.contentWindow.location;
82
- if (
83
- iframeLocation.host +
84
- iframeLocation.pathname ===
85
- windowLocation.host +
86
- windowLocation.pathname
87
- ) {
88
- target.style.display = 'none';
89
- const iframeUrlParams = new URLSearchParams(iframeLocation.search);
90
- const data: { [key: string]: string } = {};
91
- iframeUrlParams.forEach((v, k) => {
92
- data[k] = v;
93
- })
94
- if (data.error) {
95
- return reject({
96
- error: iframeUrlParams.get('error'),
97
- error_description: iframeUrlParams.get('error_description')
98
- })
99
- }
100
- return resolve(data)
101
- } else {
102
- target.style.display = 'block';
103
- }
104
- } catch (error) {
105
- target.style.display = 'block';
106
- }
107
- };
108
- })
109
- }
110
-
112
+ } catch (error) {
113
+ target.style.display = 'block';
114
+ }
115
+ };
116
+ });
117
+ };
111
118
 
112
119
  class Callback {
113
- public callFunc: (data?: { [key: string]: string }) => void;
114
- }
120
+ public callFunc: (data?: { [key: string]: string }) => void;
121
+ }
@@ -1,66 +1,68 @@
1
1
  export interface RequestOptions {
2
- body?: any | null;
3
- headers?: any | null;
4
- method?: string;
2
+ body?: any | null;
3
+ headers?: any | null;
4
+ method?: string;
5
5
 
6
- [key: string]: any;
6
+ [key: string]: any;
7
7
  }
8
8
 
9
9
  export enum ErrorType {
10
- UNREACHABLE = 'unreachable'
10
+ UNREACHABLE = 'unreachable',
11
11
  }
12
12
 
13
13
  export type RequestFn = <T>(url: string, options?: RequestOptions) => Promise<T>;
14
14
 
15
15
  /** An Error For all concern **/
16
16
  export interface ResponseError {
17
- error: string;
18
- error_description?: string;
19
- error_uri?: string;
20
- details?: any;
21
- request_id?: string;
17
+ error: string;
18
+ error_description?: string;
19
+ error_uri?: string;
20
+ details?: any;
21
+ request_id?: string;
22
22
  }
23
23
 
24
- export const defaultRequest: RequestFn = async <T>(
25
- url: string,
26
- options?: RequestOptions,
27
- ): Promise<T> => {
28
- let result: T;
29
- let responseError: ResponseError;
30
- // Objects must be copied to prevent modification of data such as body.
31
- const copyOptions = Object.assign({}, options);
32
- if (!copyOptions.method) {
33
- copyOptions.method = 'GET';
34
- }
35
- if (copyOptions.body && typeof copyOptions.body !== 'string') {
36
- copyOptions.body = JSON.stringify(copyOptions.body, (key, value) => {
37
- if (value && value !== '') {
38
- return value
39
- }
40
- })
41
- }
42
- const requestId = copyOptions.headers['x-request-id'] as string;
43
- try {
44
- const responseResult: Response = await fetch(url, copyOptions);
45
- const jsonResponse = await responseResult.json();
46
- if (jsonResponse.error || responseResult.status >= 400) {
47
- responseError = jsonResponse as ResponseError;
48
- responseError.error_uri = new URL(url).pathname;
49
- responseError.request_id = requestId;
50
- } else {
51
- result = jsonResponse as T;
24
+ export const defaultRequest: RequestFn = async <T>(url: string, options?: RequestOptions): Promise<T> => {
25
+ let result: T;
26
+ let responseError: ResponseError;
27
+ // Objects must be copied to prevent modification of data such as body.
28
+ const copyOptions = Object.assign({}, options);
29
+ if (!copyOptions.method) {
30
+ copyOptions.method = 'GET';
31
+ }
32
+ if (copyOptions.body && typeof copyOptions.body !== 'string') {
33
+ copyOptions.body = JSON.stringify(copyOptions.body, (key, value) => {
34
+ if (value) {
35
+ if (value instanceof Map) {
36
+ return Object.fromEntries(value.entries());
37
+ } else if (value !== '') {
38
+ return value;
52
39
  }
53
- } catch (error : any) {
54
- responseError = {
55
- error: ErrorType.UNREACHABLE,
56
- error_description: error.message,
57
- error_uri: new URL(url).pathname,
58
- request_id: requestId,
59
- };
60
- }
61
- if (responseError) {
62
- throw responseError;
40
+ }
41
+ });
42
+ }
43
+ const requestId = copyOptions.headers['x-request-id'] as string;
44
+ try {
45
+ copyOptions.credentials = 'include';
46
+ const responseResult: Response = await fetch(url, copyOptions);
47
+ const jsonResponse = await responseResult.json();
48
+ if (jsonResponse.error || responseResult.status >= 400) {
49
+ responseError = jsonResponse as ResponseError;
50
+ responseError.error_uri = new URL(url).pathname;
51
+ responseError.request_id = requestId;
63
52
  } else {
64
- return result as T;
53
+ result = jsonResponse as T;
65
54
  }
66
- };
55
+ } catch (error: any) {
56
+ responseError = {
57
+ error: ErrorType.UNREACHABLE,
58
+ error_description: error.message,
59
+ error_uri: new URL(url).pathname,
60
+ request_id: requestId,
61
+ };
62
+ }
63
+ if (responseError) {
64
+ throw responseError;
65
+ } else {
66
+ return result as T;
67
+ }
68
+ };