@klerick/json-api-nestjs-sdk 10.0.0-beta.2
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.
- package/CHANGELOG.md +58 -0
- package/README.md +154 -0
- package/cjs/package.json +55 -0
- package/cjs/src/index.d.ts +5 -0
- package/cjs/src/index.js +14 -0
- package/cjs/src/index.js.map +1 -0
- package/cjs/src/lib/constants/index.d.ts +1 -0
- package/cjs/src/lib/constants/index.js +5 -0
- package/cjs/src/lib/constants/index.js.map +1 -0
- package/cjs/src/lib/json-api-angular.d.ts +21 -0
- package/cjs/src/lib/json-api-angular.js +40 -0
- package/cjs/src/lib/json-api-angular.js.map +1 -0
- package/cjs/src/lib/json-api-js.d.ts +15 -0
- package/cjs/src/lib/json-api-js.js +48 -0
- package/cjs/src/lib/json-api-js.js.map +1 -0
- package/cjs/src/lib/service/atomic-operations.service.d.ts +22 -0
- package/cjs/src/lib/service/atomic-operations.service.js +86 -0
- package/cjs/src/lib/service/atomic-operations.service.js.map +1 -0
- package/cjs/src/lib/service/fetch-inner-client.d.ts +20 -0
- package/cjs/src/lib/service/fetch-inner-client.js +48 -0
- package/cjs/src/lib/service/fetch-inner-client.js.map +1 -0
- package/cjs/src/lib/service/index.d.ts +3 -0
- package/cjs/src/lib/service/index.js +7 -0
- package/cjs/src/lib/service/index.js.map +1 -0
- package/cjs/src/lib/service/json-api-sdk.service.d.ts +21 -0
- package/cjs/src/lib/service/json-api-sdk.service.js +168 -0
- package/cjs/src/lib/service/json-api-sdk.service.js.map +1 -0
- package/cjs/src/lib/service/json-api-utils.service.d.ts +28 -0
- package/cjs/src/lib/service/json-api-utils.service.js +245 -0
- package/cjs/src/lib/service/json-api-utils.service.js.map +1 -0
- package/cjs/src/lib/token/index.d.ts +4 -0
- package/cjs/src/lib/token/index.js +7 -0
- package/cjs/src/lib/token/index.js.map +1 -0
- package/cjs/src/lib/types/atomic-operation.d.ts +33 -0
- package/cjs/src/lib/types/atomic-operation.js +3 -0
- package/cjs/src/lib/types/atomic-operation.js.map +1 -0
- package/cjs/src/lib/types/atomic-type.d.ts +23 -0
- package/cjs/src/lib/types/atomic-type.js +4 -0
- package/cjs/src/lib/types/atomic-type.js.map +1 -0
- package/cjs/src/lib/types/config.d.ts +14 -0
- package/cjs/src/lib/types/config.js +3 -0
- package/cjs/src/lib/types/config.js.map +1 -0
- package/cjs/src/lib/types/filter-operand.d.ts +10 -0
- package/cjs/src/lib/types/filter-operand.js +4 -0
- package/cjs/src/lib/types/filter-operand.js.map +1 -0
- package/cjs/src/lib/types/http-inner-client.d.ts +19 -0
- package/cjs/src/lib/types/http-inner-client.js +3 -0
- package/cjs/src/lib/types/http-inner-client.js.map +1 -0
- package/cjs/src/lib/types/http-request-params.d.ts +20 -0
- package/cjs/src/lib/types/http-request-params.js +3 -0
- package/cjs/src/lib/types/http-request-params.js.map +1 -0
- package/cjs/src/lib/types/index.d.ts +9 -0
- package/cjs/src/lib/types/index.js +13 -0
- package/cjs/src/lib/types/index.js.map +1 -0
- package/cjs/src/lib/types/promise-json-api-sdk.d.ts +16 -0
- package/cjs/src/lib/types/promise-json-api-sdk.js +3 -0
- package/cjs/src/lib/types/promise-json-api-sdk.js.map +1 -0
- package/cjs/src/lib/types/query-params.d.ts +47 -0
- package/cjs/src/lib/types/query-params.js +4 -0
- package/cjs/src/lib/types/query-params.js.map +1 -0
- package/cjs/src/lib/types/utils.d.ts +6 -0
- package/cjs/src/lib/types/utils.js +3 -0
- package/cjs/src/lib/types/utils.js.map +1 -0
- package/cjs/src/lib/utils/adapter-for-axios.d.ts +3 -0
- package/cjs/src/lib/utils/adapter-for-axios.js +56 -0
- package/cjs/src/lib/utils/adapter-for-axios.js.map +1 -0
- package/cjs/src/lib/utils/entity-array.d.ts +9 -0
- package/cjs/src/lib/utils/entity-array.js +28 -0
- package/cjs/src/lib/utils/entity-array.js.map +1 -0
- package/cjs/src/lib/utils/generate-atomic-body.d.ts +22 -0
- package/cjs/src/lib/utils/generate-atomic-body.js +75 -0
- package/cjs/src/lib/utils/generate-atomic-body.js.map +1 -0
- package/cjs/src/lib/utils/http-params.d.ts +154 -0
- package/cjs/src/lib/utils/http-params.js +303 -0
- package/cjs/src/lib/utils/http-params.js.map +1 -0
- package/cjs/src/lib/utils/index.d.ts +5 -0
- package/cjs/src/lib/utils/index.js +9 -0
- package/cjs/src/lib/utils/index.js.map +1 -0
- package/cjs/src/lib/utils/utils.d.ts +4 -0
- package/cjs/src/lib/utils/utils.js +24 -0
- package/cjs/src/lib/utils/utils.js.map +1 -0
- package/cjs/src/ngModule.d.ts +4 -0
- package/cjs/src/ngModule.js +14 -0
- package/cjs/src/ngModule.js.map +1 -0
- package/mjs/package.json +57 -0
- package/mjs/src/index.d.ts +5 -0
- package/mjs/src/index.js +5 -0
- package/mjs/src/index.js.map +1 -0
- package/mjs/src/lib/constants/index.d.ts +1 -0
- package/mjs/src/lib/constants/index.js +2 -0
- package/mjs/src/lib/constants/index.js.map +1 -0
- package/mjs/src/lib/json-api-angular.d.ts +21 -0
- package/mjs/src/lib/json-api-angular.js +33 -0
- package/mjs/src/lib/json-api-angular.js.map +1 -0
- package/mjs/src/lib/json-api-js.d.ts +15 -0
- package/mjs/src/lib/json-api-js.js +44 -0
- package/mjs/src/lib/json-api-js.js.map +1 -0
- package/mjs/src/lib/service/atomic-operations.service.d.ts +22 -0
- package/mjs/src/lib/service/atomic-operations.service.js +85 -0
- package/mjs/src/lib/service/atomic-operations.service.js.map +1 -0
- package/mjs/src/lib/service/fetch-inner-client.d.ts +20 -0
- package/mjs/src/lib/service/fetch-inner-client.js +50 -0
- package/mjs/src/lib/service/fetch-inner-client.js.map +1 -0
- package/mjs/src/lib/service/index.d.ts +3 -0
- package/mjs/src/lib/service/index.js +4 -0
- package/mjs/src/lib/service/index.js.map +1 -0
- package/mjs/src/lib/service/json-api-sdk.service.d.ts +21 -0
- package/mjs/src/lib/service/json-api-sdk.service.js +174 -0
- package/mjs/src/lib/service/json-api-sdk.service.js.map +1 -0
- package/mjs/src/lib/service/json-api-utils.service.d.ts +28 -0
- package/mjs/src/lib/service/json-api-utils.service.js +253 -0
- package/mjs/src/lib/service/json-api-utils.service.js.map +1 -0
- package/mjs/src/lib/token/index.d.ts +4 -0
- package/mjs/src/lib/token/index.js +4 -0
- package/mjs/src/lib/token/index.js.map +1 -0
- package/mjs/src/lib/types/atomic-operation.d.ts +33 -0
- package/mjs/src/lib/types/atomic-operation.js +2 -0
- package/mjs/src/lib/types/atomic-operation.js.map +1 -0
- package/mjs/src/lib/types/atomic-type.d.ts +23 -0
- package/mjs/src/lib/types/atomic-type.js +2 -0
- package/mjs/src/lib/types/atomic-type.js.map +1 -0
- package/mjs/src/lib/types/config.d.ts +14 -0
- package/mjs/src/lib/types/config.js +2 -0
- package/mjs/src/lib/types/config.js.map +1 -0
- package/mjs/src/lib/types/filter-operand.d.ts +10 -0
- package/mjs/src/lib/types/filter-operand.js +2 -0
- package/mjs/src/lib/types/filter-operand.js.map +1 -0
- package/mjs/src/lib/types/http-inner-client.d.ts +19 -0
- package/mjs/src/lib/types/http-inner-client.js +2 -0
- package/mjs/src/lib/types/http-inner-client.js.map +1 -0
- package/mjs/src/lib/types/http-request-params.d.ts +20 -0
- package/mjs/src/lib/types/http-request-params.js +2 -0
- package/mjs/src/lib/types/http-request-params.js.map +1 -0
- package/mjs/src/lib/types/index.d.ts +9 -0
- package/mjs/src/lib/types/index.js +10 -0
- package/mjs/src/lib/types/index.js.map +1 -0
- package/mjs/src/lib/types/promise-json-api-sdk.d.ts +16 -0
- package/mjs/src/lib/types/promise-json-api-sdk.js +2 -0
- package/mjs/src/lib/types/promise-json-api-sdk.js.map +1 -0
- package/mjs/src/lib/types/query-params.d.ts +47 -0
- package/mjs/src/lib/types/query-params.js +2 -0
- package/mjs/src/lib/types/query-params.js.map +1 -0
- package/mjs/src/lib/types/utils.d.ts +6 -0
- package/mjs/src/lib/types/utils.js +2 -0
- package/mjs/src/lib/types/utils.js.map +1 -0
- package/mjs/src/lib/utils/adapter-for-axios.d.ts +3 -0
- package/mjs/src/lib/utils/adapter-for-axios.js +54 -0
- package/mjs/src/lib/utils/adapter-for-axios.js.map +1 -0
- package/mjs/src/lib/utils/entity-array.d.ts +9 -0
- package/mjs/src/lib/utils/entity-array.js +25 -0
- package/mjs/src/lib/utils/entity-array.js.map +1 -0
- package/mjs/src/lib/utils/generate-atomic-body.d.ts +22 -0
- package/mjs/src/lib/utils/generate-atomic-body.js +83 -0
- package/mjs/src/lib/utils/generate-atomic-body.js.map +1 -0
- package/mjs/src/lib/utils/http-params.d.ts +154 -0
- package/mjs/src/lib/utils/http-params.js +300 -0
- package/mjs/src/lib/utils/http-params.js.map +1 -0
- package/mjs/src/lib/utils/index.d.ts +5 -0
- package/mjs/src/lib/utils/index.js +6 -0
- package/mjs/src/lib/utils/index.js.map +1 -0
- package/mjs/src/lib/utils/utils.d.ts +4 -0
- package/mjs/src/lib/utils/utils.js +24 -0
- package/mjs/src/lib/utils/utils.js.map +1 -0
- package/mjs/src/ngModule.d.ts +4 -0
- package/mjs/src/ngModule.js +4 -0
- package/mjs/src/ngModule.js.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { map } from 'rxjs/operators';
|
|
3
|
+
class AxiosHttpClient {
|
|
4
|
+
axios;
|
|
5
|
+
constructor(axios) {
|
|
6
|
+
this.axios = axios;
|
|
7
|
+
}
|
|
8
|
+
observify(makeRequest, controller) {
|
|
9
|
+
return new Observable((subscriber) => {
|
|
10
|
+
makeRequest()
|
|
11
|
+
.then((response) => subscriber.next(response))
|
|
12
|
+
.catch((error) => subscriber.error(error))
|
|
13
|
+
.finally(() => subscriber.complete());
|
|
14
|
+
return { unsubscribe: () => controller.abort() };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
makeCancellable() {
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
const signal = controller.signal;
|
|
20
|
+
return {
|
|
21
|
+
controller,
|
|
22
|
+
signal,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
delete(url, body) {
|
|
26
|
+
if (body) {
|
|
27
|
+
return this.request(url, 'DELETE', body);
|
|
28
|
+
}
|
|
29
|
+
return this.request(url, 'DELETE');
|
|
30
|
+
}
|
|
31
|
+
get(url, params) {
|
|
32
|
+
return this.request(url, 'GET', undefined, params ? params.params : undefined);
|
|
33
|
+
}
|
|
34
|
+
patch(url, body) {
|
|
35
|
+
return this.request(url, 'PATCH', body);
|
|
36
|
+
}
|
|
37
|
+
post(url, body) {
|
|
38
|
+
return this.request(url, 'POST', body);
|
|
39
|
+
}
|
|
40
|
+
request(url, method, data, params) {
|
|
41
|
+
const { controller, signal } = this.makeCancellable();
|
|
42
|
+
return this.observify(() => this.axios.request({
|
|
43
|
+
url,
|
|
44
|
+
method,
|
|
45
|
+
data,
|
|
46
|
+
signal,
|
|
47
|
+
params,
|
|
48
|
+
}), controller).pipe(map((r) => r.data));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function adapterForAxios(axios) {
|
|
52
|
+
return new AxiosHttpClient(axios);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=adapter-for-axios.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-for-axios.js","sourceRoot":"","sources":["../../../../../../../../libs/json-api/json-api-nestjs-sdk/src/lib/utils/adapter-for-axios.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAYrC,MAAM,eAAe;IACC;IAApB,YAAoB,KAAY;QAAZ,UAAK,GAAL,KAAK,CAAO;IAAG,CAAC;IAE5B,SAAS,CACf,WAA6B,EAC7B,UAA2B;QAE3B,OAAO,IAAI,UAAU,CAAC,CAAC,UAAU,EAAE,EAAE;YACnC,WAAW,EAAE;iBACV,IAAI,CA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O,CACb,GAAW,EACX,MAAc,EACd,IAAQ,EACR,MAAoB;QAEpB,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,eAAe,EAAE,CAAC;QACtD,OAAO,IAAI,CAAC,SAAS,CACnB,GAAG,EAAE,CACH,IAAI,CAAC,KAAK,CAAC,OAAO,CAA4B;YAC5C,GAAG;YACH,MAAM;YACN,IAAI;YACJ,MAAM;YACN,MAAM;SACP,CAAC,EACJ,UAAU,CACX,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,UAAU,eAAe,CAAC,KAAY;IAC1C,OAAO,IAAI,eAAe,CAAC,KAAK,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PageProps } from '@klerick/json-api-nestjs-shared';
|
|
2
|
+
export declare class EntityArray<T> extends Array<T> {
|
|
3
|
+
private pageProps;
|
|
4
|
+
constructor(items: T[], pageProps: PageProps);
|
|
5
|
+
static get [Symbol.species](): ArrayConstructor;
|
|
6
|
+
get totalItems(): number;
|
|
7
|
+
get pageSize(): number;
|
|
8
|
+
get pageNumber(): number;
|
|
9
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class EntityArray extends Array {
|
|
2
|
+
pageProps;
|
|
3
|
+
constructor(items, pageProps) {
|
|
4
|
+
super(...items);
|
|
5
|
+
this.pageProps = pageProps;
|
|
6
|
+
Object.defineProperty(this, 'pageProps', {
|
|
7
|
+
writable: false,
|
|
8
|
+
enumerable: false,
|
|
9
|
+
value: pageProps,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
static get [Symbol.species]() {
|
|
13
|
+
return Array;
|
|
14
|
+
}
|
|
15
|
+
get totalItems() {
|
|
16
|
+
return this.pageProps.totalItems;
|
|
17
|
+
}
|
|
18
|
+
get pageSize() {
|
|
19
|
+
return this.pageProps.pageSize;
|
|
20
|
+
}
|
|
21
|
+
get pageNumber() {
|
|
22
|
+
return this.pageProps.pageNumber;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=entity-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-array.js","sourceRoot":"","sources":["../../../../../../../../libs/json-api/json-api-nestjs-sdk/src/lib/utils/entity-array.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAe,SAAQ,KAAQ;IACV;IAAhC,YAAY,KAAU,EAAU,SAAoB;QAClD,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC;QADc,cAAS,GAAT,SAAS,CAAW;QAElD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,WAAW,EAAE;YACvC,QAAQ,EAAE,KAAK;YACf,UAAU,EAAE,KAAK;YACjB,KAAK,EAAE,SAAS;SACjB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,KAAc,CAAC,MAAM,CAAC,OAAO,CAAC;QAClC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;IACnC,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;IACjC,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RelationKeys } from '@klerick/json-api-nestjs-shared';
|
|
2
|
+
import { AtomicMainOperations, JsonApiSdkConfig, BodyType } from '../types';
|
|
3
|
+
import { JsonApiUtilsService } from '../service';
|
|
4
|
+
export type AtomicVoidOperation = {
|
|
5
|
+
[K in keyof AtomicMainOperations<[]>]: (...arg: any) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare class GenerateAtomicBody<Entity extends object = any, Rel extends RelationKeys<Entity> = any> implements AtomicVoidOperation {
|
|
8
|
+
private jsonApiUtilsService;
|
|
9
|
+
private jsonApiSdkConfig;
|
|
10
|
+
constructor(jsonApiUtilsService: JsonApiUtilsService, jsonApiSdkConfig: JsonApiSdkConfig);
|
|
11
|
+
private bodyData;
|
|
12
|
+
private skipEmpty;
|
|
13
|
+
get isSkipEmpty(): boolean;
|
|
14
|
+
private setToBody;
|
|
15
|
+
getBody(): BodyType;
|
|
16
|
+
postOne(entity: Entity): void;
|
|
17
|
+
patchOne(entity: Entity): void;
|
|
18
|
+
deleteOne(entity: Entity, skipEmpty: boolean): void;
|
|
19
|
+
patchRelationships(entity: Entity, relationType: Rel): void;
|
|
20
|
+
postRelationships(entity: Entity, relationType: Rel): void;
|
|
21
|
+
deleteRelationships(entity: Entity, relationType: Rel): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Operation } from '@klerick/json-api-nestjs-shared';
|
|
2
|
+
import { getTypeForReq } from './utils';
|
|
3
|
+
export class GenerateAtomicBody {
|
|
4
|
+
jsonApiUtilsService;
|
|
5
|
+
jsonApiSdkConfig;
|
|
6
|
+
constructor(jsonApiUtilsService, jsonApiSdkConfig) {
|
|
7
|
+
this.jsonApiUtilsService = jsonApiUtilsService;
|
|
8
|
+
this.jsonApiSdkConfig = jsonApiSdkConfig;
|
|
9
|
+
}
|
|
10
|
+
bodyData;
|
|
11
|
+
skipEmpty = true;
|
|
12
|
+
get isSkipEmpty() {
|
|
13
|
+
return this.skipEmpty;
|
|
14
|
+
}
|
|
15
|
+
setToBody(op, entity, relationType) {
|
|
16
|
+
const type = getTypeForReq(entity.constructor.name);
|
|
17
|
+
const { relationships, attributes } = this.jsonApiUtilsService.generateBody(entity);
|
|
18
|
+
const id = Reflect.get(entity, this.jsonApiSdkConfig.idKey);
|
|
19
|
+
const idObj = op === 'add' && !relationType ? {} : { id: String(id) };
|
|
20
|
+
const data = relationType
|
|
21
|
+
? this.jsonApiUtilsService.generateRelationshipsBody(entity[relationType])
|
|
22
|
+
: {
|
|
23
|
+
type: getTypeForReq(entity.constructor.name),
|
|
24
|
+
attributes,
|
|
25
|
+
...(Object.keys(relationships).length > 0 ? { relationships } : {}),
|
|
26
|
+
...idObj,
|
|
27
|
+
};
|
|
28
|
+
const rel = relationType ? { relationship: String(relationType) } : {};
|
|
29
|
+
const tmpId = op === 'add' && id && !relationType ? { tmpId: String(id) } : {};
|
|
30
|
+
this.bodyData = {
|
|
31
|
+
op,
|
|
32
|
+
ref: { type, ...idObj, ...rel, ...tmpId },
|
|
33
|
+
...(op === 'remove' && !relationType ? {} : { data }),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
getBody() {
|
|
37
|
+
return this.bodyData;
|
|
38
|
+
}
|
|
39
|
+
postOne(entity) {
|
|
40
|
+
this.setToBody(Operation.add, entity);
|
|
41
|
+
}
|
|
42
|
+
patchOne(entity) {
|
|
43
|
+
if (Reflect.get(entity, this.jsonApiSdkConfig.idKey) === undefined) {
|
|
44
|
+
throw new Error('Resource params should be instance of resource with id params');
|
|
45
|
+
}
|
|
46
|
+
this.setToBody(Operation.update, entity);
|
|
47
|
+
}
|
|
48
|
+
deleteOne(entity, skipEmpty) {
|
|
49
|
+
if (!Reflect.get(entity, this.jsonApiSdkConfig.idKey)) {
|
|
50
|
+
throw new Error('Resource params should be instance of resource with id params');
|
|
51
|
+
}
|
|
52
|
+
this.skipEmpty = skipEmpty;
|
|
53
|
+
this.setToBody(Operation.remove, entity);
|
|
54
|
+
}
|
|
55
|
+
patchRelationships(entity, relationType) {
|
|
56
|
+
if (!Reflect.get(entity, this.jsonApiSdkConfig.idKey)) {
|
|
57
|
+
throw new Error('Resource params should be instance of resource with id params');
|
|
58
|
+
}
|
|
59
|
+
if (entity[relationType] === undefined) {
|
|
60
|
+
new Error(`${relationType.toString()} should not be undefined in entity`);
|
|
61
|
+
}
|
|
62
|
+
this.setToBody(Operation.update, entity, relationType);
|
|
63
|
+
}
|
|
64
|
+
postRelationships(entity, relationType) {
|
|
65
|
+
if (!Reflect.get(entity, this.jsonApiSdkConfig.idKey)) {
|
|
66
|
+
throw new Error('Resource params should be instance of resource with id params');
|
|
67
|
+
}
|
|
68
|
+
if (entity[relationType] === undefined) {
|
|
69
|
+
throw new Error(`${relationType.toString()} should not be undefined in entity`);
|
|
70
|
+
}
|
|
71
|
+
this.setToBody(Operation.add, entity, relationType);
|
|
72
|
+
}
|
|
73
|
+
deleteRelationships(entity, relationType) {
|
|
74
|
+
if (!Reflect.get(entity, this.jsonApiSdkConfig.idKey)) {
|
|
75
|
+
throw new Error('Resource params should be instance of resource with id params');
|
|
76
|
+
}
|
|
77
|
+
if (entity[relationType] === undefined) {
|
|
78
|
+
throw new Error(`${relationType.toString()} should not be undefined in entity`);
|
|
79
|
+
}
|
|
80
|
+
this.setToBody(Operation.remove, entity, relationType);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=generate-atomic-body.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-atomic-body.js","sourceRoot":"","sources":["../../../../../../../../libs/json-api/json-api-nestjs-sdk/src/lib/utils/generate-atomic-body.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,MAAM,iCAAiC,CAAC;AAG1E,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAOxC,MAAM,OAAO,kBAAkB;IAMnB;IACA;IAFV,YACU,mBAAwC,EACxC,gBAAkC;QADlC,wBAAmB,GAAnB,mBAAmB,CAAqB;QACxC,qBAAgB,GAAhB,gBAAgB,CAAkB;IACzC,CAAC;IACI,QAAQ,CAAY;IACpB,SAAS,GAAG,IAAI,CAAC;IAEzB,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAEO,SAAS,CAAC,EAAa,EAAE,MAAc,EAAE,YAAkB;QACjE,MAAM,IAAI,GAAG,aAAa,CAAC,MAAM,CAAC,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C,CAAC,CAAC;QAC5E,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;IACzD,CAAC;IACD,iBAAiB,CAAC,MAAc,EAAE,YAAiB;QACjD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YACtD,MAAM,IAAI,KAAK,CACb,+DAA+D,CAChE,CAAC;QACJ,CAAC;QAED,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CACb,GAAG,YAAY,CAAC,QAAQ,EAAE,oCAAoC,CAC/D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;IACtD,CAAC;IACD,mBAAmB,CAAC,MAAc,EAAE,YAAiB;QACnD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YACtD,MAAM,IAAI,KAAK,CACb,+DAA+D,CAChE,CAAC;QACJ,CAAC;QAED,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CACb,GAAG,YAAY,CAAC,QAAQ,EAAE,oCAAoC,CAC/D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;IACzD,CAAC;CACF"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
6
|
+
* found in the LICENSE file at https://angular.io/license
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* A codec for encoding and decoding parameters in URLs.
|
|
10
|
+
*
|
|
11
|
+
* Used by `HttpParams`.
|
|
12
|
+
*
|
|
13
|
+
* @publicApi
|
|
14
|
+
**/
|
|
15
|
+
export interface HttpParameterCodec {
|
|
16
|
+
encodeKey(key: string): string;
|
|
17
|
+
encodeValue(value: string): string;
|
|
18
|
+
decodeKey(key: string): string;
|
|
19
|
+
decodeValue(value: string): string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Provides encoding and decoding of URL parameter and query-string values.
|
|
23
|
+
*
|
|
24
|
+
* Serializes and parses URL parameter keys and values to encode and decode them.
|
|
25
|
+
* If you pass URL query parameters without encoding,
|
|
26
|
+
* the query parameters can be misinterpreted at the receiving end.
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* @publicApi
|
|
30
|
+
*/
|
|
31
|
+
export declare class HttpUrlEncodingCodec implements HttpParameterCodec {
|
|
32
|
+
/**
|
|
33
|
+
* Encodes a key name for a URL parameter or query-string.
|
|
34
|
+
* @param key The key name.
|
|
35
|
+
* @returns The encoded key name.
|
|
36
|
+
*/
|
|
37
|
+
encodeKey(key: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Encodes the value of a URL parameter or query-string.
|
|
40
|
+
* @param value The value.
|
|
41
|
+
* @returns The encoded value.
|
|
42
|
+
*/
|
|
43
|
+
encodeValue(value: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Decodes an encoded URL parameter or query-string key.
|
|
46
|
+
* @param key The encoded key name.
|
|
47
|
+
* @returns The decoded key name.
|
|
48
|
+
*/
|
|
49
|
+
decodeKey(key: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Decodes an encoded URL parameter or query-string value.
|
|
52
|
+
* @param value The encoded value.
|
|
53
|
+
* @returns The decoded value.
|
|
54
|
+
*/
|
|
55
|
+
decodeValue(value: string): string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Options used to construct an `HttpParams` instance.
|
|
59
|
+
*
|
|
60
|
+
* @publicApi
|
|
61
|
+
*/
|
|
62
|
+
export interface HttpParamsOptions {
|
|
63
|
+
/**
|
|
64
|
+
* String representation of the HTTP parameters in URL-query-string format.
|
|
65
|
+
* Mutually exclusive with `fromObject`.
|
|
66
|
+
*/
|
|
67
|
+
fromString?: string;
|
|
68
|
+
/** Object map of the HTTP parameters. Mutually exclusive with `fromString`. */
|
|
69
|
+
fromObject?: ParamObject;
|
|
70
|
+
/** Encoding codec used to parse and serialize the parameters. */
|
|
71
|
+
encoder?: HttpParameterCodec;
|
|
72
|
+
}
|
|
73
|
+
export type ParamObject = {
|
|
74
|
+
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* An HTTP request/response body that represents serialized parameters,
|
|
78
|
+
* per the MIME type `application/x-www-form-urlencoded`.
|
|
79
|
+
*
|
|
80
|
+
* This class is immutable; all mutation operations return a new instance.
|
|
81
|
+
*
|
|
82
|
+
* @publicApi
|
|
83
|
+
*/
|
|
84
|
+
export declare class HttpParams {
|
|
85
|
+
private map;
|
|
86
|
+
private encoder;
|
|
87
|
+
private updates;
|
|
88
|
+
private cloneFrom;
|
|
89
|
+
constructor(options?: HttpParamsOptions);
|
|
90
|
+
/**
|
|
91
|
+
* Reports whether the body includes one or more values for a given parameter.
|
|
92
|
+
* @param param The parameter name.
|
|
93
|
+
* @returns True if the parameter has one or more values,
|
|
94
|
+
* false if it has no value or is not present.
|
|
95
|
+
*/
|
|
96
|
+
has(param: string): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Retrieves the first value for a parameter.
|
|
99
|
+
* @param param The parameter name.
|
|
100
|
+
* @returns The first value of the given parameter,
|
|
101
|
+
* or `null` if the parameter is not present.
|
|
102
|
+
*/
|
|
103
|
+
get(param: string): string | null;
|
|
104
|
+
/**
|
|
105
|
+
* Retrieves all values for a parameter.
|
|
106
|
+
* @param param The parameter name.
|
|
107
|
+
* @returns All values in a string array,
|
|
108
|
+
* or `null` if the parameter not present.
|
|
109
|
+
*/
|
|
110
|
+
getAll(param: string): string[] | null;
|
|
111
|
+
/**
|
|
112
|
+
* Retrieves all the parameters for this body.
|
|
113
|
+
* @returns The parameter names in a string array.
|
|
114
|
+
*/
|
|
115
|
+
keys(): string[];
|
|
116
|
+
/**
|
|
117
|
+
* Appends a new value to existing values for a parameter.
|
|
118
|
+
* @param param The parameter name.
|
|
119
|
+
* @param value The new value to add.
|
|
120
|
+
* @return A new body with the appended value.
|
|
121
|
+
*/
|
|
122
|
+
append(param: string, value: string | number | boolean): HttpParams;
|
|
123
|
+
/**
|
|
124
|
+
* Constructs a new body with appended values for the given parameter name.
|
|
125
|
+
* @param params parameters and values
|
|
126
|
+
* @return A new body with the new value.
|
|
127
|
+
*/
|
|
128
|
+
appendAll(params: {
|
|
129
|
+
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
|
|
130
|
+
}): HttpParams;
|
|
131
|
+
/**
|
|
132
|
+
* Replaces the value for a parameter.
|
|
133
|
+
* @param param The parameter name.
|
|
134
|
+
* @param value The new value.
|
|
135
|
+
* @return A new body with the new value.
|
|
136
|
+
*/
|
|
137
|
+
set(param: string, value: string | number | boolean): HttpParams;
|
|
138
|
+
/**
|
|
139
|
+
* Removes a given value or all values from a parameter.
|
|
140
|
+
* @param param The parameter name.
|
|
141
|
+
* @param value The value to remove, if provided.
|
|
142
|
+
* @return A new body with the given value removed, or with all values
|
|
143
|
+
* removed if no value is specified.
|
|
144
|
+
*/
|
|
145
|
+
delete(param: string, value?: string | number | boolean): HttpParams;
|
|
146
|
+
/**
|
|
147
|
+
* Serializes the body to an encoded string, where key-value pairs (separated by `=`) are
|
|
148
|
+
* separated by `&`s.
|
|
149
|
+
*/
|
|
150
|
+
toString(): string;
|
|
151
|
+
toObject(): ParamObject;
|
|
152
|
+
private clone;
|
|
153
|
+
private init;
|
|
154
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright Google LLC All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Use of this source code is governed by an MIT-style license that can be
|
|
6
|
+
* found in the LICENSE file at https://angular.io/license
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Provides encoding and decoding of URL parameter and query-string values.
|
|
10
|
+
*
|
|
11
|
+
* Serializes and parses URL parameter keys and values to encode and decode them.
|
|
12
|
+
* If you pass URL query parameters without encoding,
|
|
13
|
+
* the query parameters can be misinterpreted at the receiving end.
|
|
14
|
+
*
|
|
15
|
+
*
|
|
16
|
+
* @publicApi
|
|
17
|
+
*/
|
|
18
|
+
export class HttpUrlEncodingCodec {
|
|
19
|
+
/**
|
|
20
|
+
* Encodes a key name for a URL parameter or query-string.
|
|
21
|
+
* @param key The key name.
|
|
22
|
+
* @returns The encoded key name.
|
|
23
|
+
*/
|
|
24
|
+
encodeKey(key) {
|
|
25
|
+
return standardEncoding(key);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Encodes the value of a URL parameter or query-string.
|
|
29
|
+
* @param value The value.
|
|
30
|
+
* @returns The encoded value.
|
|
31
|
+
*/
|
|
32
|
+
encodeValue(value) {
|
|
33
|
+
return standardEncoding(value);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Decodes an encoded URL parameter or query-string key.
|
|
37
|
+
* @param key The encoded key name.
|
|
38
|
+
* @returns The decoded key name.
|
|
39
|
+
*/
|
|
40
|
+
decodeKey(key) {
|
|
41
|
+
return decodeURIComponent(key);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Decodes an encoded URL parameter or query-string value.
|
|
45
|
+
* @param value The encoded value.
|
|
46
|
+
* @returns The decoded value.
|
|
47
|
+
*/
|
|
48
|
+
decodeValue(value) {
|
|
49
|
+
return decodeURIComponent(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function paramParser(rawParams, codec) {
|
|
53
|
+
const map = new Map();
|
|
54
|
+
if (rawParams.length > 0) {
|
|
55
|
+
// The `window.location.search` can be used while creating an instance of the `HttpParams` class
|
|
56
|
+
// (e.g. `new HttpParams({ fromString: window.location.search })`). The `window.location.search`
|
|
57
|
+
// may start with the `?` char, so we strip it if it's present.
|
|
58
|
+
const params = rawParams.replace(/^\?/, '').split('&');
|
|
59
|
+
params.forEach((param) => {
|
|
60
|
+
const eqIdx = param.indexOf('=');
|
|
61
|
+
const [key, val] = eqIdx == -1
|
|
62
|
+
? [codec.decodeKey(param), '']
|
|
63
|
+
: [
|
|
64
|
+
codec.decodeKey(param.slice(0, eqIdx)),
|
|
65
|
+
codec.decodeValue(param.slice(eqIdx + 1)),
|
|
66
|
+
];
|
|
67
|
+
const list = map.get(key) || [];
|
|
68
|
+
list.push(val);
|
|
69
|
+
map.set(key, list);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return map;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Encode input string with standard encodeURIComponent and then un-encode specific characters.
|
|
76
|
+
*/
|
|
77
|
+
const STANDARD_ENCODING_REGEX = /%(\d[a-f0-9])/gi;
|
|
78
|
+
const STANDARD_ENCODING_REPLACEMENTS = {
|
|
79
|
+
'40': '@',
|
|
80
|
+
'3A': ':',
|
|
81
|
+
'24': '$',
|
|
82
|
+
'2C': ',',
|
|
83
|
+
'3B': ';',
|
|
84
|
+
'3D': '=',
|
|
85
|
+
'3F': '?',
|
|
86
|
+
'2F': '/',
|
|
87
|
+
};
|
|
88
|
+
function standardEncoding(v) {
|
|
89
|
+
return encodeURIComponent(v).replace(STANDARD_ENCODING_REGEX, (s, t) => STANDARD_ENCODING_REPLACEMENTS[t] ?? s);
|
|
90
|
+
}
|
|
91
|
+
function valueToString(value) {
|
|
92
|
+
return `${value}`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* An HTTP request/response body that represents serialized parameters,
|
|
96
|
+
* per the MIME type `application/x-www-form-urlencoded`.
|
|
97
|
+
*
|
|
98
|
+
* This class is immutable; all mutation operations return a new instance.
|
|
99
|
+
*
|
|
100
|
+
* @publicApi
|
|
101
|
+
*/
|
|
102
|
+
export class HttpParams {
|
|
103
|
+
map;
|
|
104
|
+
encoder;
|
|
105
|
+
updates = null;
|
|
106
|
+
cloneFrom = null;
|
|
107
|
+
constructor(options = {}) {
|
|
108
|
+
this.encoder = options.encoder || new HttpUrlEncodingCodec();
|
|
109
|
+
if (!!options.fromString) {
|
|
110
|
+
if (!!options.fromObject) {
|
|
111
|
+
throw new Error(`Cannot specify both fromString and fromObject.`);
|
|
112
|
+
}
|
|
113
|
+
this.map = paramParser(options.fromString, this.encoder);
|
|
114
|
+
}
|
|
115
|
+
else if (!!options.fromObject) {
|
|
116
|
+
this.map = new Map();
|
|
117
|
+
Object.keys(options.fromObject).forEach((key) => {
|
|
118
|
+
const value = options.fromObject[key];
|
|
119
|
+
// convert the values to strings
|
|
120
|
+
const values = Array.isArray(value)
|
|
121
|
+
? value.map(valueToString)
|
|
122
|
+
: [valueToString(value)];
|
|
123
|
+
this.map.set(key, values);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.map = null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Reports whether the body includes one or more values for a given parameter.
|
|
132
|
+
* @param param The parameter name.
|
|
133
|
+
* @returns True if the parameter has one or more values,
|
|
134
|
+
* false if it has no value or is not present.
|
|
135
|
+
*/
|
|
136
|
+
has(param) {
|
|
137
|
+
this.init();
|
|
138
|
+
return this.map.has(param);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Retrieves the first value for a parameter.
|
|
142
|
+
* @param param The parameter name.
|
|
143
|
+
* @returns The first value of the given parameter,
|
|
144
|
+
* or `null` if the parameter is not present.
|
|
145
|
+
*/
|
|
146
|
+
get(param) {
|
|
147
|
+
this.init();
|
|
148
|
+
const res = this.map.get(param);
|
|
149
|
+
return !!res ? res[0] : null;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Retrieves all values for a parameter.
|
|
153
|
+
* @param param The parameter name.
|
|
154
|
+
* @returns All values in a string array,
|
|
155
|
+
* or `null` if the parameter not present.
|
|
156
|
+
*/
|
|
157
|
+
getAll(param) {
|
|
158
|
+
this.init();
|
|
159
|
+
return this.map.get(param) || null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Retrieves all the parameters for this body.
|
|
163
|
+
* @returns The parameter names in a string array.
|
|
164
|
+
*/
|
|
165
|
+
keys() {
|
|
166
|
+
this.init();
|
|
167
|
+
return Array.from(this.map.keys());
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Appends a new value to existing values for a parameter.
|
|
171
|
+
* @param param The parameter name.
|
|
172
|
+
* @param value The new value to add.
|
|
173
|
+
* @return A new body with the appended value.
|
|
174
|
+
*/
|
|
175
|
+
append(param, value) {
|
|
176
|
+
return this.clone({ param, value, op: 'a' });
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Constructs a new body with appended values for the given parameter name.
|
|
180
|
+
* @param params parameters and values
|
|
181
|
+
* @return A new body with the new value.
|
|
182
|
+
*/
|
|
183
|
+
appendAll(params) {
|
|
184
|
+
const updates = [];
|
|
185
|
+
Object.keys(params).forEach((param) => {
|
|
186
|
+
const value = params[param];
|
|
187
|
+
if (Array.isArray(value)) {
|
|
188
|
+
value.forEach((_value) => {
|
|
189
|
+
updates.push({ param, value: _value, op: 'a' });
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
updates.push({
|
|
194
|
+
param,
|
|
195
|
+
value: value,
|
|
196
|
+
op: 'a',
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return this.clone(updates);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Replaces the value for a parameter.
|
|
204
|
+
* @param param The parameter name.
|
|
205
|
+
* @param value The new value.
|
|
206
|
+
* @return A new body with the new value.
|
|
207
|
+
*/
|
|
208
|
+
set(param, value) {
|
|
209
|
+
return this.clone({ param, value, op: 's' });
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Removes a given value or all values from a parameter.
|
|
213
|
+
* @param param The parameter name.
|
|
214
|
+
* @param value The value to remove, if provided.
|
|
215
|
+
* @return A new body with the given value removed, or with all values
|
|
216
|
+
* removed if no value is specified.
|
|
217
|
+
*/
|
|
218
|
+
delete(param, value) {
|
|
219
|
+
return this.clone({ param, value, op: 'd' });
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Serializes the body to an encoded string, where key-value pairs (separated by `=`) are
|
|
223
|
+
* separated by `&`s.
|
|
224
|
+
*/
|
|
225
|
+
toString() {
|
|
226
|
+
this.init();
|
|
227
|
+
return (this.keys()
|
|
228
|
+
.map((key) => {
|
|
229
|
+
const eKey = this.encoder.encodeKey(key);
|
|
230
|
+
// `a: ['1']` produces `'a=1'`
|
|
231
|
+
// `b: []` produces `''`
|
|
232
|
+
// `c: ['1', '2']` produces `'c=1&c=2'`
|
|
233
|
+
return this.map.get(key)
|
|
234
|
+
.map((value) => eKey + '=' + this.encoder.encodeValue(value))
|
|
235
|
+
.join('&');
|
|
236
|
+
})
|
|
237
|
+
// filter out empty values because `b: []` produces `''`
|
|
238
|
+
// which results in `a=1&&c=1&c=2` instead of `a=1&c=1&c=2` if we don't
|
|
239
|
+
.filter((param) => param !== '')
|
|
240
|
+
.join('&'));
|
|
241
|
+
}
|
|
242
|
+
toObject() {
|
|
243
|
+
return this.keys().reduce((acum, key) => {
|
|
244
|
+
const val = this.get(key);
|
|
245
|
+
if (val)
|
|
246
|
+
acum[key] = val;
|
|
247
|
+
return acum;
|
|
248
|
+
}, {});
|
|
249
|
+
}
|
|
250
|
+
clone(update) {
|
|
251
|
+
const clone = new HttpParams({
|
|
252
|
+
encoder: this.encoder,
|
|
253
|
+
});
|
|
254
|
+
clone.cloneFrom = this.cloneFrom || this;
|
|
255
|
+
clone.updates = (this.updates || []).concat(update);
|
|
256
|
+
return clone;
|
|
257
|
+
}
|
|
258
|
+
init() {
|
|
259
|
+
if (this.map === null) {
|
|
260
|
+
this.map = new Map();
|
|
261
|
+
}
|
|
262
|
+
if (this.cloneFrom !== null) {
|
|
263
|
+
this.cloneFrom.init();
|
|
264
|
+
this.cloneFrom
|
|
265
|
+
.keys()
|
|
266
|
+
.forEach((key) => this.map.set(key, this.cloneFrom.map.get(key)));
|
|
267
|
+
this.updates.forEach((update) => {
|
|
268
|
+
switch (update.op) {
|
|
269
|
+
case 'a':
|
|
270
|
+
case 's':
|
|
271
|
+
const base = (update.op === 'a' ? this.map.get(update.param) : undefined) ||
|
|
272
|
+
[];
|
|
273
|
+
base.push(valueToString(update.value));
|
|
274
|
+
this.map.set(update.param, base);
|
|
275
|
+
break;
|
|
276
|
+
case 'd':
|
|
277
|
+
if (update.value !== undefined) {
|
|
278
|
+
let base = this.map.get(update.param) || [];
|
|
279
|
+
const idx = base.indexOf(valueToString(update.value));
|
|
280
|
+
if (idx !== -1) {
|
|
281
|
+
base.splice(idx, 1);
|
|
282
|
+
}
|
|
283
|
+
if (base.length > 0) {
|
|
284
|
+
this.map.set(update.param, base);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
this.map.delete(update.param);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
this.map.delete(update.param);
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
this.cloneFrom = this.updates = null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
//# sourceMappingURL=http-params.js.map
|