@omerlo/omerlo-webkit 0.0.31 → 0.0.33

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,4 +1,4 @@
1
- import { requestPublisher } from '../utils/request';
1
+ import { requestOmerlo } from '../utils/request';
2
2
  export const integrationFetchers = (f) => {
3
3
  return {
4
4
  getReference: getReference(f)
@@ -7,7 +7,7 @@ export const integrationFetchers = (f) => {
7
7
  export function getReference(f) {
8
8
  return async (key) => {
9
9
  const opts = { parser: parseReference };
10
- return requestPublisher(f, `/references/${key}`, opts);
10
+ return requestOmerlo(f, `/core/v2/references/${key}`, opts);
11
11
  };
12
12
  }
13
13
  export function parseReference(data, _assocs) {
@@ -64,6 +64,10 @@ export const proxyHook = async ({ event, resolve }) => {
64
64
  event.url.pathname = event.url.pathname.replace('/api/publisher/', `/api/public/publisher/v2/`);
65
65
  return handleApiProxy({ event, resolve });
66
66
  }
67
+ if (event.url.pathname.startsWith('/api/omerlo/')) {
68
+ event.url.pathname = event.url.pathname.replace('/api/omerlo/', '/api/public/');
69
+ return handleApiProxy({ event, resolve });
70
+ }
67
71
  return resolve(event);
68
72
  };
69
73
  export const handleUserToken = async ({ event, resolve }) => {
@@ -52,7 +52,8 @@ const applicationToken = {
52
52
  accessToken: '',
53
53
  refreshToken: '',
54
54
  expiredAt: 0,
55
- init: false
55
+ init: false,
56
+ refreshErrorCounter: 0
56
57
  };
57
58
  /**
58
59
  * Get the token used by the application.
@@ -66,16 +67,36 @@ export async function getApplicationToken() {
66
67
  }
67
68
  return applicationToken.accessToken;
68
69
  }
70
+ let refreshingPromise = null;
69
71
  async function refreshApplicationToken() {
70
72
  if (!applicationToken.refreshToken) {
71
73
  throw new Error('Could not refresh the application token because the refresh token is null');
72
74
  }
73
- const token = await refresh(applicationToken.refreshToken);
74
- applicationToken.accessToken = token.accessToken;
75
- applicationToken.refreshToken = token.refreshToken;
76
- const date = new Date();
77
- const timestamps = date.setSeconds(date.getSeconds() + token.expiresIn - 60);
78
- applicationToken.expiredAt = timestamps;
75
+ if (refreshingPromise) {
76
+ return refreshingPromise;
77
+ }
78
+ refreshingPromise = (async () => {
79
+ try {
80
+ const token = await refresh(applicationToken.refreshToken);
81
+ applicationToken.accessToken = token.accessToken;
82
+ applicationToken.refreshToken = token.refreshToken;
83
+ const date = new Date();
84
+ const timestamps = date.setSeconds(date.getSeconds() + token.expiresIn - 60);
85
+ applicationToken.expiredAt = timestamps;
86
+ applicationToken.refreshErrorCounter = 0;
87
+ }
88
+ catch (e) {
89
+ applicationToken.refreshErrorCounter += 1;
90
+ if (applicationToken.refreshErrorCounter >= 10) {
91
+ applicationToken.init = false;
92
+ }
93
+ throw e;
94
+ }
95
+ finally {
96
+ refreshingPromise = null;
97
+ }
98
+ })();
99
+ return refreshingPromise;
79
100
  }
80
101
  async function newApplicationToken() {
81
102
  const token = await getAnonymousToken('application');
@@ -16,4 +16,10 @@ export declare function dirtyRequest(f: typeof fetch, path: string, opts: DirtyF
16
16
  */
17
17
  export declare function requestPublisher<T>(f: typeof fetch, path: string, opts: Partial<FetchOptions<T>>): Promise<ApiResponse<T>>;
18
18
  export declare function dirtyRequestPublisher(f: typeof fetch, path: string, opts: DirtyFetchOptions): Promise<Response>;
19
+ /**
20
+ * NOTE: This is for OLD api and will be removed once every API will
21
+ * be developped in Reader.
22
+ */
23
+ export declare function requestOmerlo<T>(f: typeof fetch, path: string, opts: Partial<FetchOptions<T>>): Promise<ApiResponse<T>>;
24
+ export declare function dirtyRequestOmerlo(f: typeof fetch, path: string, opts: DirtyFetchOptions): Promise<Response>;
19
25
  export {};
@@ -62,3 +62,24 @@ export async function dirtyRequestPublisher(f, path, opts) {
62
62
  }
63
63
  return f(path.toString(), opts);
64
64
  }
65
+ /**
66
+ * NOTE: This is for OLD api and will be removed once every API will
67
+ * be developped in Reader.
68
+ */
69
+ export async function requestOmerlo(f, path, opts) {
70
+ const { body, headers, method, queryParams, parser } = parseRequestOpts(opts);
71
+ return dirtyRequestOmerlo(f, path, { body, headers, method, queryParams }).then(async (resp) => {
72
+ return parseApiResponse(resp, parser);
73
+ });
74
+ }
75
+ export async function dirtyRequestOmerlo(f, path, opts) {
76
+ const queryParams = new URLSearchParams();
77
+ path = `/api/omerlo/${path}`;
78
+ if (opts?.queryParams) {
79
+ Object.entries(opts.queryParams).forEach(([key, value]) => {
80
+ queryParams.append(key, String(value));
81
+ });
82
+ path = `${path}?${queryParams}`;
83
+ }
84
+ return f(path.toString(), opts);
85
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omerlo/omerlo-webkit",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",