@basmilius/http-client 1.13.0 → 2.1.0
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/README.md +12 -34
- package/dist/decorator/dto/clone.d.ts +0 -3
- package/dist/decorator/dto/fill.d.ts +0 -3
- package/dist/decorator/dto/helper/areEqual.d.ts +0 -4
- package/dist/decorator/dto/helper/assertDto.d.ts +0 -3
- package/dist/decorator/dto/helper/cloneDto.d.ts +0 -3
- package/dist/decorator/dto/helper/executeIfDtoDirtyAndMarkClean.d.ts +0 -3
- package/dist/decorator/dto/helper/isDto.d.ts +0 -3
- package/dist/decorator/dto/helper/isDtoClean.d.ts +0 -3
- package/dist/decorator/dto/helper/isDtoDirty.d.ts +0 -3
- package/dist/decorator/dto/helper/relateDtoTo.d.ts +0 -3
- package/dist/decorator/dto/helper/relateValueTo.d.ts +0 -3
- package/dist/decorator/dto/helper/trackDto.d.ts +0 -3
- package/dist/decorator/dto/helper/unrelateDtoFrom.d.ts +0 -3
- package/dist/decorator/dto/helper/unrelateValueFrom.d.ts +0 -3
- package/dist/decorator/dto/index.d.ts +1 -4
- package/dist/decorator/dto/toJSON.d.ts +0 -3
- package/dist/decorator/index.d.ts +1 -1
- package/dist/index.js +4 -0
- package/dist/{http-client.js.map → index.js.map} +2 -2
- package/package.json +14 -13
- package/src/adapter/HttpAdapter.ts +67 -0
- package/src/adapter/index.ts +1 -0
- package/src/decorator/adapter.ts +10 -0
- package/src/decorator/bound.ts +5 -0
- package/src/decorator/debounce.ts +30 -0
- package/src/decorator/dto/arrayProxy.ts +65 -0
- package/src/decorator/dto/classProxy.ts +49 -0
- package/src/decorator/dto/clone.ts +24 -0
- package/src/decorator/dto/constant.ts +7 -0
- package/src/decorator/dto/fill.ts +18 -0
- package/src/decorator/dto/helper/areEqual.ts +13 -0
- package/src/decorator/dto/helper/assertDto.ts +11 -0
- package/src/decorator/dto/helper/circularProtect.ts +35 -0
- package/src/decorator/dto/helper/cloneDto.ts +9 -0
- package/src/decorator/dto/helper/executeIfDtoDirtyAndMarkClean.ts +16 -0
- package/src/decorator/dto/helper/index.ts +37 -0
- package/src/decorator/dto/helper/instance.ts +5 -0
- package/src/decorator/dto/helper/isDto.ts +9 -0
- package/src/decorator/dto/helper/isDtoClean.ts +10 -0
- package/src/decorator/dto/helper/isDtoDirty.ts +10 -0
- package/src/decorator/dto/helper/markDtoClean.ts +29 -0
- package/src/decorator/dto/helper/markDtoDirty.ts +26 -0
- package/src/decorator/dto/helper/relateDtoTo.ts +13 -0
- package/src/decorator/dto/helper/relateValueTo.ts +22 -0
- package/src/decorator/dto/helper/trackDto.ts +13 -0
- package/src/decorator/dto/helper/triggerDto.ts +18 -0
- package/src/decorator/dto/helper/unrelateDtoFrom.ts +15 -0
- package/src/decorator/dto/helper/unrelateValueFrom.ts +22 -0
- package/src/decorator/dto/index.ts +78 -0
- package/src/decorator/dto/instance.ts +33 -0
- package/src/decorator/dto/instanceProxy.ts +89 -0
- package/src/decorator/dto/map.ts +4 -0
- package/src/decorator/dto/refProxy.ts +61 -0
- package/src/decorator/dto/serialize/deserialize.ts +5 -0
- package/src/decorator/dto/serialize/deserializeArray.ts +5 -0
- package/src/decorator/dto/serialize/deserializeDateTime.ts +6 -0
- package/src/decorator/dto/serialize/deserializeDto.ts +28 -0
- package/src/decorator/dto/serialize/deserializeObject.ts +9 -0
- package/src/decorator/dto/serialize/deserializeUnknown.ts +31 -0
- package/src/decorator/dto/serialize/index.ts +7 -0
- package/src/decorator/dto/serialize/isSerializedDateTime.ts +5 -0
- package/src/decorator/dto/serialize/isSerializedDto.ts +5 -0
- package/src/decorator/dto/serialize/serialize.ts +5 -0
- package/src/decorator/dto/serialize/serializeArray.ts +19 -0
- package/src/decorator/dto/serialize/serializeDateTime.ts +9 -0
- package/src/decorator/dto/serialize/serializeDto.ts +21 -0
- package/src/decorator/dto/serialize/serializeObject.ts +9 -0
- package/src/decorator/dto/serialize/serializeUnknown.ts +34 -0
- package/src/decorator/dto/serialize/types.ts +3 -0
- package/src/decorator/dto/serialize/uuid.ts +3 -0
- package/src/decorator/dto/symbols.ts +11 -0
- package/src/decorator/dto/toJSON.ts +20 -0
- package/src/decorator/index.ts +31 -0
- package/src/dto/BlobResponse.ts +20 -0
- package/src/dto/Paginated.ts +38 -0
- package/src/dto/RequestError.ts +33 -0
- package/src/dto/ValidationError.ts +38 -0
- package/src/dto/index.ts +4 -0
- package/src/http/BaseResponse.ts +31 -0
- package/src/http/BaseService.ts +8 -0
- package/src/http/HttpClient.ts +41 -0
- package/src/http/QueryString.ts +75 -0
- package/src/http/RequestBuilder.ts +222 -0
- package/src/http/helpers.ts +17 -0
- package/src/http/index.ts +19 -0
- package/src/index.ts +5 -0
- package/src/type/index.ts +74 -0
- package/LICENSE +0 -21
- package/dist/http-client.js +0 -1095
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ENABLE_SERIALIZATION_LOGGING } from '../constant';
|
|
2
|
+
import { DTO_CLASS_MAP } from '../map';
|
|
3
|
+
import type { SerializedDto } from './types';
|
|
4
|
+
import type DtoInstance from '../instance';
|
|
5
|
+
import deserializeArray from './deserializeArray';
|
|
6
|
+
import deserializeObject from './deserializeObject';
|
|
7
|
+
|
|
8
|
+
const CACHE: Record<string, unknown> = {};
|
|
9
|
+
|
|
10
|
+
export default function ([, id, name, state, args]: SerializedDto): DtoInstance<unknown> {
|
|
11
|
+
if (!(name in DTO_CLASS_MAP)) {
|
|
12
|
+
throw new Error(`Cannot restore @dto. Dto ${name} was not found.`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (id in CACHE) {
|
|
16
|
+
return CACHE[id] as DtoInstance<unknown>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
ENABLE_SERIALIZATION_LOGGING && console.group('⭐️', name, id);
|
|
20
|
+
const Dto = DTO_CLASS_MAP[name];
|
|
21
|
+
const instance = new Dto(...deserializeArray(args));
|
|
22
|
+
instance.fill(deserializeObject(state));
|
|
23
|
+
ENABLE_SERIALIZATION_LOGGING && console.groupEnd();
|
|
24
|
+
|
|
25
|
+
CACHE[id] = instance;
|
|
26
|
+
|
|
27
|
+
return instance;
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import deserializeUnknown from './deserializeUnknown';
|
|
2
|
+
|
|
3
|
+
export default function (obj: object): Record<string, unknown> {
|
|
4
|
+
return Object.fromEntries(
|
|
5
|
+
Object.entries(obj)
|
|
6
|
+
.map(([key, value]) => [key, deserializeUnknown(value)])
|
|
7
|
+
.filter(([, value]) => value !== undefined)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import deserializeArray from './deserializeArray';
|
|
2
|
+
import deserializeDateTime from './deserializeDateTime';
|
|
3
|
+
import deserializeDto from './deserializeDto';
|
|
4
|
+
import deserializeObject from './deserializeObject';
|
|
5
|
+
import isSerializedDateTime from './isSerializedDateTime';
|
|
6
|
+
import isSerializedDto from './isSerializedDto';
|
|
7
|
+
|
|
8
|
+
export default function (obj: unknown): unknown {
|
|
9
|
+
switch (true) {
|
|
10
|
+
case obj === null:
|
|
11
|
+
return null;
|
|
12
|
+
|
|
13
|
+
case Array.isArray(obj):
|
|
14
|
+
switch (true) {
|
|
15
|
+
case isSerializedDateTime(obj):
|
|
16
|
+
return deserializeDateTime(obj);
|
|
17
|
+
|
|
18
|
+
case isSerializedDto(obj):
|
|
19
|
+
return deserializeDto(obj);
|
|
20
|
+
|
|
21
|
+
default:
|
|
22
|
+
return deserializeArray(obj);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
case typeof obj === 'object':
|
|
26
|
+
return deserializeObject(obj);
|
|
27
|
+
|
|
28
|
+
default:
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import type { Serialized } from './types';
|
|
3
|
+
import isDto from '../helper/isDto';
|
|
4
|
+
import serializeDateTime from './serializeDateTime';
|
|
5
|
+
import serializeDto from './serializeDto';
|
|
6
|
+
import serializeUnknown from './serializeUnknown';
|
|
7
|
+
|
|
8
|
+
export default function (obj: unknown[]): Serialized[] {
|
|
9
|
+
switch (true) {
|
|
10
|
+
case obj.every(isDto):
|
|
11
|
+
return obj.map(serializeDto);
|
|
12
|
+
|
|
13
|
+
case obj.every(DateTime.isDateTime):
|
|
14
|
+
return obj.map(serializeDateTime);
|
|
15
|
+
|
|
16
|
+
default:
|
|
17
|
+
return obj.map(serializeUnknown);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { toRaw } from 'vue';
|
|
2
|
+
import { ARGS, NAME } from '../symbols';
|
|
3
|
+
import type { SerializedDto } from './types';
|
|
4
|
+
import type DtoInstance from '../instance';
|
|
5
|
+
import serializeArray from './serializeArray';
|
|
6
|
+
import serializeObject from './serializeObject';
|
|
7
|
+
import uuid from './uuid';
|
|
8
|
+
|
|
9
|
+
export default function (obj: DtoInstance<unknown>): SerializedDto {
|
|
10
|
+
obj = toRaw(obj);
|
|
11
|
+
|
|
12
|
+
const json = obj.toJSON();
|
|
13
|
+
|
|
14
|
+
return [
|
|
15
|
+
0xBF1,
|
|
16
|
+
uuid(),
|
|
17
|
+
obj[NAME],
|
|
18
|
+
serializeObject(json),
|
|
19
|
+
serializeArray(obj[ARGS])
|
|
20
|
+
];
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import serializeUnknown from './serializeUnknown';
|
|
2
|
+
|
|
3
|
+
export default function (obj: object): Record<string, unknown> {
|
|
4
|
+
return Object.fromEntries(
|
|
5
|
+
Object.entries(obj)
|
|
6
|
+
.map(([key, value]) => [key, serializeUnknown(value)])
|
|
7
|
+
.filter(([, value]) => value !== undefined)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import type { Serialized } from './types';
|
|
3
|
+
import isDto from '../helper/isDto';
|
|
4
|
+
import serializeArray from './serializeArray';
|
|
5
|
+
import serializeDateTime from './serializeDateTime';
|
|
6
|
+
import serializeDto from './serializeDto';
|
|
7
|
+
import serializeObject from './serializeObject';
|
|
8
|
+
|
|
9
|
+
export default function (obj: unknown): Serialized | undefined {
|
|
10
|
+
switch (true) {
|
|
11
|
+
case obj === null:
|
|
12
|
+
return null;
|
|
13
|
+
|
|
14
|
+
case Array.isArray(obj):
|
|
15
|
+
return serializeArray(obj);
|
|
16
|
+
|
|
17
|
+
case isDto(obj):
|
|
18
|
+
return serializeDto(obj);
|
|
19
|
+
|
|
20
|
+
case DateTime.isDateTime(obj):
|
|
21
|
+
return serializeDateTime(obj);
|
|
22
|
+
|
|
23
|
+
case typeof obj === 'object':
|
|
24
|
+
return serializeObject(obj);
|
|
25
|
+
|
|
26
|
+
case typeof obj === 'boolean':
|
|
27
|
+
case typeof obj === 'number':
|
|
28
|
+
case typeof obj === 'string':
|
|
29
|
+
return obj;
|
|
30
|
+
|
|
31
|
+
default:
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const ARGS: unique symbol = Symbol();
|
|
2
|
+
export const CHILDREN: unique symbol = Symbol();
|
|
3
|
+
export const DESCRIPTORS: unique symbol = Symbol();
|
|
4
|
+
export const DIRTY: unique symbol = Symbol();
|
|
5
|
+
export const NAME: unique symbol = Symbol();
|
|
6
|
+
export const PARENT: unique symbol = Symbol();
|
|
7
|
+
export const PARENT_KEY: unique symbol = Symbol();
|
|
8
|
+
export const PROPERTIES: unique symbol = Symbol();
|
|
9
|
+
export const PROXY: unique symbol = Symbol();
|
|
10
|
+
export const TRACK: unique symbol = Symbol();
|
|
11
|
+
export const TRIGGER: unique symbol = Symbol();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isDto } from './helper';
|
|
2
|
+
import { PROPERTIES } from './symbols';
|
|
3
|
+
import type DtoInstance from './instance';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the json object representation of the dto.
|
|
7
|
+
*/
|
|
8
|
+
export default function (this: DtoInstance<unknown>): Record<string, unknown> {
|
|
9
|
+
return Object.fromEntries(
|
|
10
|
+
this[PROPERTIES].map(property => {
|
|
11
|
+
let value: unknown = Reflect.get.call(this, this, property, this);
|
|
12
|
+
|
|
13
|
+
if (isDto(value)) {
|
|
14
|
+
value = value.toJSON();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return [property, value];
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import adapter from './adapter';
|
|
2
|
+
import bound from './bound';
|
|
3
|
+
import debounce from './debounce';
|
|
4
|
+
import dto from './dto';
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
adapter,
|
|
8
|
+
bound,
|
|
9
|
+
debounce,
|
|
10
|
+
dto
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
assertDto,
|
|
15
|
+
cloneDto,
|
|
16
|
+
executeIfDtoDirtyAndMarkClean,
|
|
17
|
+
isDto,
|
|
18
|
+
isDtoClean,
|
|
19
|
+
isDtoDirty,
|
|
20
|
+
markDtoClean,
|
|
21
|
+
markDtoDirty
|
|
22
|
+
} from './dto/helper';
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
deserialize,
|
|
26
|
+
serialize
|
|
27
|
+
} from './dto/serialize';
|
|
28
|
+
|
|
29
|
+
export type {
|
|
30
|
+
DtoInstance
|
|
31
|
+
} from './dto';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { dto } from '../decorator';
|
|
2
|
+
|
|
3
|
+
@dto
|
|
4
|
+
export default class BlobResponse {
|
|
5
|
+
get blob(): Blob {
|
|
6
|
+
return this.#blob;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get name(): string {
|
|
10
|
+
return this.#name;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
readonly #blob: Blob;
|
|
14
|
+
readonly #name: string;
|
|
15
|
+
|
|
16
|
+
constructor(blob: Blob, name: string) {
|
|
17
|
+
this.#blob = blob;
|
|
18
|
+
this.#name = name;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { dto } from '../decorator';
|
|
2
|
+
|
|
3
|
+
@dto
|
|
4
|
+
export default class Paginated<T> {
|
|
5
|
+
get items(): T[] {
|
|
6
|
+
return this.#items;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get page(): number {
|
|
10
|
+
return this.#page;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get pageSize(): number {
|
|
14
|
+
return this.#pageSize;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get pages(): number {
|
|
18
|
+
return this.#pages;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get totalItems(): number {
|
|
22
|
+
return this.#totalItems;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
readonly #items: T[];
|
|
26
|
+
readonly #page: number;
|
|
27
|
+
readonly #pageSize: number;
|
|
28
|
+
readonly #pages: number;
|
|
29
|
+
readonly #totalItems: number;
|
|
30
|
+
|
|
31
|
+
constructor(items: T[], page: number, pageSize: number, pages: number, totalItems: number) {
|
|
32
|
+
this.#items = items;
|
|
33
|
+
this.#page = page;
|
|
34
|
+
this.#pageSize = pageSize;
|
|
35
|
+
this.#pages = pages;
|
|
36
|
+
this.#totalItems = totalItems;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { dto } from '../decorator';
|
|
2
|
+
import type { HttpStatusCode } from '../type';
|
|
3
|
+
|
|
4
|
+
@dto
|
|
5
|
+
export default class RequestError {
|
|
6
|
+
get code(): number {
|
|
7
|
+
return this.#code;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get error(): string {
|
|
11
|
+
return this.#error;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get errorDescription(): string {
|
|
15
|
+
return this.#errorDescription;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get statusCode(): HttpStatusCode {
|
|
19
|
+
return this.#statusCode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
readonly #code: number;
|
|
23
|
+
readonly #error: string;
|
|
24
|
+
readonly #errorDescription: string;
|
|
25
|
+
readonly #statusCode: HttpStatusCode;
|
|
26
|
+
|
|
27
|
+
constructor(code: number, error: string, errorDescription: string, statusCode: HttpStatusCode) {
|
|
28
|
+
this.#code = code;
|
|
29
|
+
this.#error = error;
|
|
30
|
+
this.#errorDescription = errorDescription;
|
|
31
|
+
this.#statusCode = statusCode;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { dto } from '../decorator';
|
|
2
|
+
|
|
3
|
+
@dto
|
|
4
|
+
export default class ValidationError {
|
|
5
|
+
get code(): number {
|
|
6
|
+
return this.#code;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get error(): string {
|
|
10
|
+
return this.#error;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get errorDescription(): string {
|
|
14
|
+
return this.#errorDescription;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get errors(): Record<string, ValidationError> {
|
|
18
|
+
return this.#errors;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get params(): Record<string, string | number | boolean> {
|
|
22
|
+
return this.#params;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
readonly #code: number;
|
|
26
|
+
readonly #error: string;
|
|
27
|
+
readonly #errorDescription: string;
|
|
28
|
+
readonly #errors: Record<string, ValidationError>;
|
|
29
|
+
readonly #params: Record<string, string | number | boolean>;
|
|
30
|
+
|
|
31
|
+
constructor(code: number, error: string, errorDescription: string, errors?: Record<string, ValidationError>, params?: Record<string, string | number | boolean>) {
|
|
32
|
+
this.#code = code;
|
|
33
|
+
this.#error = error;
|
|
34
|
+
this.#errorDescription = errorDescription;
|
|
35
|
+
this.#errors = errors;
|
|
36
|
+
this.#params = params;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/dto/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HttpStatusCode } from '../type';
|
|
2
|
+
|
|
3
|
+
export default class BaseResponse<T> {
|
|
4
|
+
get data(): T {
|
|
5
|
+
return this.#data;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get headers(): Headers {
|
|
9
|
+
return this.#response.headers;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get ok(): boolean {
|
|
13
|
+
return this.statusCode >= 200 && this.statusCode < 300;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get response(): Response {
|
|
17
|
+
return this.#response;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get statusCode(): HttpStatusCode {
|
|
21
|
+
return this.#response.status as HttpStatusCode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
readonly #data: T;
|
|
25
|
+
readonly #response: Response;
|
|
26
|
+
|
|
27
|
+
constructor(data: T, response: Response) {
|
|
28
|
+
this.#data = data;
|
|
29
|
+
this.#response = response;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export default class HttpClient {
|
|
2
|
+
get authToken(): string | null {
|
|
3
|
+
return this.#authToken;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
set authToken(value: string | null) {
|
|
7
|
+
this.#authToken = value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get baseUrl(): string {
|
|
11
|
+
return this.#baseUrl;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get dataField(): boolean {
|
|
15
|
+
return this.#dataField;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
#authToken: string | null;
|
|
19
|
+
readonly #baseUrl: string;
|
|
20
|
+
readonly #dataField: boolean;
|
|
21
|
+
|
|
22
|
+
constructor(authToken: string | null, baseUrl: string, dataField: boolean = false) {
|
|
23
|
+
this.#authToken = authToken;
|
|
24
|
+
this.#baseUrl = baseUrl;
|
|
25
|
+
this.#dataField = dataField;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static get instance(): HttpClient {
|
|
29
|
+
if (HttpClient.#instance === null) {
|
|
30
|
+
throw new Error('There is currently no HttpClient instance registered. Register one using the HttpClient.register() function.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return HttpClient.#instance;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static #instance: HttpClient | null = null;
|
|
37
|
+
|
|
38
|
+
static register(client: HttpClient): void {
|
|
39
|
+
HttpClient.#instance = client;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export default class QueryString {
|
|
2
|
+
readonly #builder: URLSearchParams;
|
|
3
|
+
|
|
4
|
+
constructor() {
|
|
5
|
+
this.#builder = new URLSearchParams();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
public build(): string {
|
|
9
|
+
return this.#builder.toString();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public append(name: string, value: QueryStringValue): QueryString {
|
|
13
|
+
return this.#add(this.#builder.append.bind(this.#builder), name, value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public appendArray(name: string, values: QueryStringValue[] | null): QueryString {
|
|
17
|
+
if (values === undefined || values === null) {
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
values.forEach(value => this.append(name, value));
|
|
22
|
+
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public delete(name: string): QueryString {
|
|
27
|
+
this.#builder.delete(name);
|
|
28
|
+
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public get(name: string): string | null {
|
|
33
|
+
return this.#builder.get(name);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public getAll(name: string): string[] {
|
|
37
|
+
return this.#builder.getAll(name);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public has(name: string): boolean {
|
|
41
|
+
return this.#builder.has(name);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public set(name: string, value: QueryStringValue): QueryString {
|
|
45
|
+
return this.#add(this.#builder.set.bind(this.#builder), name, value);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#add(fn: ((name: string, value: string) => void), name: string, value: QueryStringValue): QueryString {
|
|
49
|
+
if (!value && value !== false) {
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
switch (typeof value) {
|
|
54
|
+
case 'boolean':
|
|
55
|
+
fn(name, value ? 'true' : 'false');
|
|
56
|
+
break;
|
|
57
|
+
|
|
58
|
+
case 'number':
|
|
59
|
+
fn(name, value.toString(10));
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
case 'string':
|
|
63
|
+
fn(name, value);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public static builder(): QueryString {
|
|
71
|
+
return new QueryString();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type QueryStringValue = boolean | number | string | null;
|