@omerlo/omerlo-webkit 0.0.3 → 0.0.4
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.
|
@@ -27,14 +27,18 @@ export function parseTopicSummary(data, _assocs) {
|
|
|
27
27
|
export function subscribeToTopic(f) {
|
|
28
28
|
return (params) => {
|
|
29
29
|
const body = { push_token: params.pushToken };
|
|
30
|
-
const
|
|
30
|
+
const headers = new Headers();
|
|
31
|
+
headers.set('Content-Type', 'application/json');
|
|
32
|
+
const opts = { method: 'post', body, headers };
|
|
31
33
|
return request(f, `/topics/${params.topicId}/subscribe`, opts);
|
|
32
34
|
};
|
|
33
35
|
}
|
|
34
36
|
export function unsubscribeFromTopic(f) {
|
|
35
37
|
return (params) => {
|
|
36
38
|
const body = { push_token: params.pushToken };
|
|
37
|
-
const
|
|
39
|
+
const headers = new Headers();
|
|
40
|
+
headers.set('Content-Type', 'application/json');
|
|
41
|
+
const opts = { method: 'post', body, headers };
|
|
38
42
|
return request(f, `/topics/${params.topicId}/unsubscribe`, opts);
|
|
39
43
|
};
|
|
40
44
|
}
|
|
@@ -7,17 +7,19 @@ import { refresh } from "./token";
|
|
|
7
7
|
const handleApiProxy = async ({ event, ...tail }) => {
|
|
8
8
|
event.url.host = env.PRIVATE_OMERLO_HOST;
|
|
9
9
|
event.url.protocol = env.PRIVATE_OMERLO_PROTOCOL;
|
|
10
|
-
event.
|
|
11
|
-
event.request.headers.set('x-omerlo-media-id', env.PRIVATE_OMERLO_MEDIA_ID);
|
|
10
|
+
event.url.port = ''; // This to prevent custom posts when working with localhost
|
|
12
11
|
let accessToken = event.locals.accessToken;
|
|
13
12
|
if (!accessToken) {
|
|
14
13
|
accessToken = await getApplicationToken();
|
|
15
14
|
}
|
|
16
|
-
event.request.
|
|
15
|
+
const body = event.request.body;
|
|
16
|
+
const method = event.request.method;
|
|
17
|
+
const headers = new Headers();
|
|
18
|
+
headers.set('Content-Type', event.request.headers.get('content-type') ?? 'application/json');
|
|
19
|
+
headers.set('x-omerlo-media-id', env.PRIVATE_OMERLO_MEDIA_ID ?? '');
|
|
20
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
17
21
|
return await fetch(event.url.toString(), {
|
|
18
|
-
body
|
|
19
|
-
method: event.request.method,
|
|
20
|
-
headers: event.request.headers,
|
|
22
|
+
body, headers, method,
|
|
21
23
|
duplex: 'half'
|
|
22
24
|
})
|
|
23
25
|
.then(async (resp) => {
|
|
@@ -2,14 +2,16 @@ import type { ApiAssocs, ApiData, ApiResponse } from './api';
|
|
|
2
2
|
type FetchOptions<T> = {
|
|
3
3
|
parser?: (data: ApiData, assocs: ApiAssocs) => T;
|
|
4
4
|
queryParams?: ApiData;
|
|
5
|
-
method?:
|
|
5
|
+
method?: 'get' | 'post' | 'put' | 'delete';
|
|
6
6
|
body?: ApiData;
|
|
7
|
+
headers?: Headers;
|
|
7
8
|
};
|
|
8
9
|
export declare function request<T>(f: typeof fetch, path: string, opts: FetchOptions<T>): Promise<ApiResponse<T>>;
|
|
9
10
|
type DirtyFetchOptions = {
|
|
10
11
|
queryParams?: ApiData;
|
|
11
|
-
method?:
|
|
12
|
+
method?: 'get' | 'post' | 'put' | 'delete';
|
|
12
13
|
body?: ApiData;
|
|
14
|
+
headers?: Headers;
|
|
13
15
|
};
|
|
14
|
-
export declare function dirtyRequest(f: typeof fetch, path: string, opts
|
|
16
|
+
export declare function dirtyRequest(f: typeof fetch, path: string, opts: DirtyFetchOptions): Promise<Response>;
|
|
15
17
|
export {};
|
|
@@ -2,7 +2,17 @@ import { parseApiResponse } from './api';
|
|
|
2
2
|
import { BROWSER } from 'esm-env';
|
|
3
3
|
export async function request(f, path, opts) {
|
|
4
4
|
const parser = opts.parser || ((data) => data);
|
|
5
|
-
|
|
5
|
+
const body = JSON.stringify(opts.body);
|
|
6
|
+
const headers = opts.headers;
|
|
7
|
+
const method = opts.method ?? 'get';
|
|
8
|
+
const queryParams = opts.queryParams;
|
|
9
|
+
// Enforce JSON content type for posts
|
|
10
|
+
if (!opts.headers) {
|
|
11
|
+
opts.headers = new Headers();
|
|
12
|
+
}
|
|
13
|
+
if (['post', 'put'].includes(method) && !opts.headers.get('Content-Type'))
|
|
14
|
+
opts.headers.set('Content-Type', 'application/json');
|
|
15
|
+
return dirtyRequest(f, path, { body, headers, method, queryParams }).then(async (resp) => {
|
|
6
16
|
return parseApiResponse(resp, parser);
|
|
7
17
|
});
|
|
8
18
|
}
|
|
@@ -15,8 +25,7 @@ export async function dirtyRequest(f, path, opts) {
|
|
|
15
25
|
});
|
|
16
26
|
path = `${path}?${queryParams}`;
|
|
17
27
|
}
|
|
18
|
-
const
|
|
19
|
-
const resp = await f(path.toString(), { method: opts.method, body: JSON.stringify(opts.body), headers });
|
|
28
|
+
const resp = await f(path.toString(), opts);
|
|
20
29
|
if (BROWSER && resp.headers.get('x-logout') == 'true') {
|
|
21
30
|
const webkitComponent = document.getElementById('omerlo-webkit');
|
|
22
31
|
if (webkitComponent) {
|