-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path-utils.ts
109 lines (102 loc) · 2.94 KB
/
-utils.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @module @ember-data/json-api/request
*/
import { BuildURLConfig, setBuildURLConfig as setConfig } from '@ember-data/request-utils';
import { type UrlOptions } from '@ember-data/request-utils';
import type { CacheOptions, ConstrainedRequestOptions } from '@warp-drive/core-types/request';
export interface JSONAPIConfig extends BuildURLConfig {
profiles?: {
pagination?: string;
[key: string]: string | undefined;
};
extensions?: {
atomic?: string;
[key: string]: string | undefined;
};
}
const JsonApiAccept = 'application/vnd.api+json';
const DEFAULT_CONFIG: JSONAPIConfig = { host: '', namespace: '' };
export let CONFIG: JSONAPIConfig = DEFAULT_CONFIG;
export let ACCEPT_HEADER_VALUE = 'application/vnd.api+json';
/**
* Allows setting extensions and profiles to be used in the `Accept` header.
*
* Extensions and profiles are keyed by their namespace with the value being
* their URI.
*
* Example:
*
* ```ts
* setBuildURLConfig({
* extensions: {
* atomic: 'https://jsonapi.org/ext/atomic'
* },
* profiles: {
* pagination: 'https://jsonapi.org/profiles/ethanresnick/cursor-pagination'
* }
* });
*
* This also sets the global configuration for `buildBaseURL`
* for host and namespace values for the application
* in the `@ember-data/request-utils` package.
*
* These values may still be overridden by passing
* them to buildBaseURL directly.
*
* This method may be called as many times as needed
*
* ```ts
* type BuildURLConfig = {
* host: string;
* namespace: string'
* }
* ```
*
* @method setBuildURLConfig
* @static
* @public
* @for @ember-data/json-api/request
* @param {BuildURLConfig} config
* @returns void
*/
export function setBuildURLConfig(config: JSONAPIConfig): void {
CONFIG = Object.assign({}, DEFAULT_CONFIG, config);
if (config.profiles || config.extensions) {
let accept = JsonApiAccept;
if (config.profiles) {
const profiles = Object.values(config.profiles);
if (profiles.length) {
accept += ';profile="' + profiles.join(' ') + '"';
}
}
if (config.extensions) {
const extensions = Object.values(config.extensions);
if (extensions.length) {
accept += ';ext=' + extensions.join(' ');
}
}
ACCEPT_HEADER_VALUE = accept;
}
setConfig(config);
}
export function copyForwardUrlOptions(urlOptions: UrlOptions, options: ConstrainedRequestOptions): void {
if ('host' in options) {
urlOptions.host = options.host;
}
if ('namespace' in options) {
urlOptions.namespace = options.namespace;
}
if ('resourcePath' in options) {
urlOptions.resourcePath = options.resourcePath;
}
}
export function extractCacheOptions(options: ConstrainedRequestOptions) {
const cacheOptions: CacheOptions = {};
if ('reload' in options) {
cacheOptions.reload = options.reload;
}
if ('backgroundReload' in options) {
cacheOptions.backgroundReload = options.backgroundReload;
}
return cacheOptions;
}