|
| 1 | +/** |
| 2 | + * @module @ember-data/json-api/request |
| 3 | + */ |
| 4 | +import { BuildURLConfig, setBuildURLConfig as setConfig } from '@ember-data/request-utils'; |
1 | 5 | import { type UrlOptions } from '@ember-data/request-utils';
|
2 | 6 | import type { CacheOptions, ConstrainedRequestOptions } from '@warp-drive/core-types/request';
|
3 | 7 |
|
| 8 | +export interface JSONAPIConfig extends BuildURLConfig { |
| 9 | + profiles?: { |
| 10 | + pagination?: string; |
| 11 | + [key: string]: string | undefined; |
| 12 | + }; |
| 13 | + extensions?: { |
| 14 | + atomic?: string; |
| 15 | + [key: string]: string | undefined; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +const JsonApiAccept = 'application/vnd.api+json'; |
| 20 | +const DEFAULT_CONFIG: JSONAPIConfig = { host: '', namespace: '' }; |
| 21 | +export let CONFIG: JSONAPIConfig = DEFAULT_CONFIG; |
| 22 | +export let ACCEPT_HEADER_VALUE = 'application/vnd.api+json'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Allows setting extensions and profiles to be used in the `Accept` header. |
| 26 | + * |
| 27 | + * Extensions and profiles are keyed by their namespace with the value being |
| 28 | + * their URI. |
| 29 | + * |
| 30 | + * Example: |
| 31 | + * |
| 32 | + * ```ts |
| 33 | + * setBuildURLConfig({ |
| 34 | + * extensions: { |
| 35 | + * atomic: 'https://jsonapi.org/ext/atomic' |
| 36 | + * }, |
| 37 | + * profiles: { |
| 38 | + * pagination: 'https://jsonapi.org/profiles/ethanresnick/cursor-pagination' |
| 39 | + * } |
| 40 | + * }); |
| 41 | + * |
| 42 | + * This also sets the global configuration for `buildBaseURL` |
| 43 | + * for host and namespace values for the application |
| 44 | + * in the `@ember-data/request-utils` package. |
| 45 | + * |
| 46 | + * These values may still be overridden by passing |
| 47 | + * them to buildBaseURL directly. |
| 48 | + * |
| 49 | + * This method may be called as many times as needed |
| 50 | + * |
| 51 | + * ```ts |
| 52 | + * type BuildURLConfig = { |
| 53 | + * host: string; |
| 54 | + * namespace: string' |
| 55 | + * } |
| 56 | + * ``` |
| 57 | + * |
| 58 | + * @method setBuildURLConfig |
| 59 | + * @static |
| 60 | + * @public |
| 61 | + * @for @ember-data/json-api/request |
| 62 | + * @param {BuildURLConfig} config |
| 63 | + * @returns void |
| 64 | + */ |
| 65 | +export function setBuildURLConfig(config: JSONAPIConfig): void { |
| 66 | + CONFIG = Object.assign({}, DEFAULT_CONFIG, config); |
| 67 | + |
| 68 | + if (config.profiles || config.extensions) { |
| 69 | + let accept = JsonApiAccept; |
| 70 | + if (config.profiles) { |
| 71 | + const profiles = Object.values(config.profiles); |
| 72 | + if (profiles.length) { |
| 73 | + accept += ';profile="' + profiles.join(' ') + '"'; |
| 74 | + } |
| 75 | + } |
| 76 | + if (config.extensions) { |
| 77 | + const extensions = Object.values(config.extensions); |
| 78 | + if (extensions.length) { |
| 79 | + accept += ';ext=' + extensions.join(' '); |
| 80 | + } |
| 81 | + } |
| 82 | + ACCEPT_HEADER_VALUE = accept; |
| 83 | + } |
| 84 | + |
| 85 | + setConfig(config); |
| 86 | +} |
| 87 | + |
4 | 88 | export function copyForwardUrlOptions(urlOptions: UrlOptions, options: ConstrainedRequestOptions): void {
|
5 | 89 | if ('host' in options) {
|
6 | 90 | urlOptions.host = options.host;
|
|
0 commit comments