-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalConfig.ts.txt
More file actions
94 lines (76 loc) · 2.59 KB
/
globalConfig.ts.txt
File metadata and controls
94 lines (76 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
export type RequestPayloadMethod =
| 'GET'
| 'HEAD'
| 'POST'
| 'PUT'
| 'DELETE'
| 'CONNECT'
| 'OPTIONS'
| 'TRACE'
| 'PATCH';
export interface RequestPayload {
method?: RequestPayloadMethod;
query?: Record<string, any>;
body?: any;
unwrap?: boolean;
forceBlob?: boolean;
}
export interface ServiceConfig {
// Your API-Key, this is optional in the sense of if you omit this, and you're in a browser, the
// cookie-authentication (include-credentials) will be used.
key?: string;
// Your domain, if omitted location.host will be used (browser env).
host?: string;
// If you want to use https, defaults to location.protocol (browser env).
secure?: boolean;
// If you want that some and count requests are bundled into multi requests.
multiRequest?: boolean;
// If you want that the ignoreMissingProperties parameter to be set to true for every post request.
ignoreMissingProperties?: boolean;
// Optional request/response interceptors.
interceptors?: {
// Takes the generated request, you can either return a new request,
// a response (which will be taken as "the" response) or nothing.
// The payload contains the raw input generated by the SDK.
request?: (
request: Request,
payload: RequestPayload
) => Request | Response | void | Promise<Request | Response | void>;
// Takes the response. This can either be the one from the server or an
// artificially-crafted one by the request interceptor.
response?: (response: Response) => Response | void | Promise<Response | void>;
};
// Whether POST should be used instead of GET for some() and count() operations
usePost?: boolean;
}
export interface RequestOptions {
signal?: AbortSignal;
}
type ServiceConfigWithoutMultiRequest = Omit<ServiceConfig, 'multiRequest'>;
let globalConfig: ServiceConfig | undefined;
export const getGlobalConfig = (): ServiceConfig | undefined => globalConfig;
export const setGlobalConfig = (cfg?: ServiceConfig) => (globalConfig = cfg);
export const getHost = (cfg: ServiceConfig) => {
let host = cfg.host?.replace(/^https?:\/\//, '');
if (!host && typeof location !== 'undefined') {
host = location.host;
}
if (!host) {
throw new Error('Please specify a host');
}
return host;
};
export const getProtocol = (cfg: ServiceConfig) => {
const protocol =
cfg.secure !== undefined
? cfg.secure
? 'https:'
: 'http:'
: typeof location !== 'undefined'
? location.protocol
: undefined;
if (!protocol) {
throw new Error('Please specify a protocol (secure)');
}
return protocol;
};