@azure-net/kit 0.2.5 → 0.3.1
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/dist/core/datasource/BaseDatasource.d.ts +15 -4
- package/dist/core/datasource/BaseDatasource.js +10 -4
- package/dist/core/httpService/HttpService.d.ts +3 -3
- package/dist/core/httpService/HttpService.js +2 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/query/QueryBuilder.d.ts +23 -0
- package/dist/core/query/QueryBuilder.js +53 -0
- package/dist/core/query/index.d.ts +1 -0
- package/dist/core/query/index.js +1 -0
- package/dist/lib/onClickOutside/OnClickOutside.d.ts +1 -1
- package/dist/lib/onClickOutside/OnClickOutside.js +3 -0
- package/package.json +2 -2
|
@@ -1,19 +1,30 @@
|
|
|
1
1
|
import { HttpService, HttpServiceResponse } from '../httpService/HttpService.js';
|
|
2
|
-
|
|
2
|
+
import { QueryBuilder } from '../query/index.js';
|
|
3
|
+
export interface IHttpDatasource {
|
|
3
4
|
/**
|
|
4
5
|
* Executes a wrapped HTTP request and handles the response.
|
|
5
6
|
*
|
|
6
|
-
* @param callback - A function that receives the HttpService and
|
|
7
|
+
* @param callback - A function that receives the HttpService and QueryBuilder. Returns a Promise of a response.
|
|
7
8
|
* @returns A typed HTTP response wrapped in a Promise.
|
|
8
9
|
*/
|
|
9
10
|
createRequest<T>(callback: <J = T>(params: {
|
|
10
11
|
http: HttpService;
|
|
12
|
+
query: QueryBuilder;
|
|
11
13
|
}) => Promise<HttpServiceResponse<J>>): Promise<HttpServiceResponse<T>>;
|
|
12
14
|
}
|
|
13
|
-
export declare class
|
|
15
|
+
export declare class BaseHttpDatasource implements IHttpDatasource {
|
|
14
16
|
protected readonly httpClient: HttpService;
|
|
15
|
-
|
|
17
|
+
protected readonly query: QueryBuilder;
|
|
18
|
+
constructor(params: {
|
|
19
|
+
http?: HttpService;
|
|
20
|
+
query?: QueryBuilder;
|
|
21
|
+
});
|
|
16
22
|
createRequest<T>(callback: <J = T>(params: {
|
|
17
23
|
http: HttpService;
|
|
24
|
+
query: QueryBuilder;
|
|
18
25
|
}) => Promise<HttpServiceResponse<J>>): Promise<HttpServiceResponse<T>>;
|
|
19
26
|
}
|
|
27
|
+
export declare const createHttpDatasource: (params: {
|
|
28
|
+
http?: HttpService;
|
|
29
|
+
query?: QueryBuilder;
|
|
30
|
+
}) => BaseHttpDatasource;
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { HttpService, HttpServiceError, HttpServiceResponse } from '../httpService/HttpService.js';
|
|
2
|
-
|
|
2
|
+
import { QueryBuilder } from '../query/index.js';
|
|
3
|
+
export class BaseHttpDatasource {
|
|
3
4
|
httpClient;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
query;
|
|
6
|
+
constructor(params) {
|
|
7
|
+
this.httpClient = params.http ?? new HttpService({ baseUrl: '' });
|
|
8
|
+
this.query = params.query ?? new QueryBuilder();
|
|
6
9
|
}
|
|
7
10
|
async createRequest(callback) {
|
|
8
|
-
return await callback({ http: this.httpClient })
|
|
11
|
+
return await callback({ http: this.httpClient, query: this.query })
|
|
9
12
|
.then((response) => response)
|
|
10
13
|
.catch((err) => {
|
|
11
14
|
throw err;
|
|
12
15
|
});
|
|
13
16
|
}
|
|
14
17
|
}
|
|
18
|
+
export const createHttpDatasource = (params) => {
|
|
19
|
+
return new BaseHttpDatasource(params);
|
|
20
|
+
};
|
|
@@ -5,7 +5,7 @@ export interface IHttpServiceResponse<T = unknown> {
|
|
|
5
5
|
status: number;
|
|
6
6
|
success: boolean;
|
|
7
7
|
data: T;
|
|
8
|
-
message
|
|
8
|
+
message: string;
|
|
9
9
|
}
|
|
10
10
|
export interface IHttpServiceError<T = unknown> extends IHttpServiceResponse<T> {
|
|
11
11
|
original?: Error;
|
|
@@ -15,7 +15,7 @@ export declare class HttpServiceResponse<T> implements IHttpServiceResponse<T> {
|
|
|
15
15
|
status: number;
|
|
16
16
|
success: boolean;
|
|
17
17
|
data: T;
|
|
18
|
-
message
|
|
18
|
+
message: string;
|
|
19
19
|
constructor({ headers, status, success, data, message }: IHttpServiceResponse<T>);
|
|
20
20
|
}
|
|
21
21
|
export declare class HttpServiceError<T> implements IHttpServiceError<T> {
|
|
@@ -23,7 +23,7 @@ export declare class HttpServiceError<T> implements IHttpServiceError<T> {
|
|
|
23
23
|
status: number;
|
|
24
24
|
success: boolean;
|
|
25
25
|
data: T;
|
|
26
|
-
message
|
|
26
|
+
message: string;
|
|
27
27
|
original?: Error;
|
|
28
28
|
constructor({ headers, status, success, data, message, original }: IHttpServiceError<T>);
|
|
29
29
|
}
|
|
@@ -126,7 +126,8 @@ export class HttpService {
|
|
|
126
126
|
status: err?.response?.status ?? 500,
|
|
127
127
|
headers,
|
|
128
128
|
success: false,
|
|
129
|
-
original: err
|
|
129
|
+
original: err,
|
|
130
|
+
message: 'Request failed'
|
|
130
131
|
});
|
|
131
132
|
httpServiceEventBus.publish('HttpServiceError', error);
|
|
132
133
|
return this.errorHandler ? this.errorHandler(error) : error;
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type ArrayFormat = 'repeat' | 'brackets' | 'comma' | 'json';
|
|
2
|
+
export type ObjectFormat = 'default' | 'nested-brackets';
|
|
3
|
+
export interface IQueryBuilder {
|
|
4
|
+
build(params: Record<string, unknown>, opts?: {
|
|
5
|
+
arrayFormat?: ArrayFormat;
|
|
6
|
+
objectFormat?: ObjectFormat;
|
|
7
|
+
}): string;
|
|
8
|
+
}
|
|
9
|
+
export declare class QueryBuilder implements IQueryBuilder {
|
|
10
|
+
private readonly defaultArrayFormat;
|
|
11
|
+
private readonly defaultObjectFormat;
|
|
12
|
+
constructor(options?: {
|
|
13
|
+
arrayFormat?: ArrayFormat;
|
|
14
|
+
objectFormat?: ObjectFormat;
|
|
15
|
+
});
|
|
16
|
+
build(params: Record<string, unknown>, opts?: {
|
|
17
|
+
delimiter?: boolean;
|
|
18
|
+
arrayFormat?: ArrayFormat;
|
|
19
|
+
objectFormat?: ObjectFormat;
|
|
20
|
+
}): string;
|
|
21
|
+
private serialize;
|
|
22
|
+
private encodeArray;
|
|
23
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class QueryBuilder {
|
|
2
|
+
defaultArrayFormat;
|
|
3
|
+
defaultObjectFormat;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.defaultArrayFormat = options?.arrayFormat ?? 'repeat';
|
|
6
|
+
this.defaultObjectFormat = options?.objectFormat ?? 'default';
|
|
7
|
+
}
|
|
8
|
+
build(params, opts) {
|
|
9
|
+
const arrayFormat = opts?.arrayFormat ?? this.defaultArrayFormat;
|
|
10
|
+
const objectFormat = opts?.objectFormat ?? this.defaultObjectFormat;
|
|
11
|
+
const parts = this.serialize(params, arrayFormat, objectFormat);
|
|
12
|
+
const delimiter = opts?.delimiter ?? true;
|
|
13
|
+
return delimiter ? `?${parts.join('&')}` : parts.join('&');
|
|
14
|
+
}
|
|
15
|
+
serialize(obj, arrayFormat, objectFormat, prefix = '') {
|
|
16
|
+
const parts = [];
|
|
17
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
18
|
+
if (value === null || value === undefined)
|
|
19
|
+
continue;
|
|
20
|
+
const fullKey = prefix ? (objectFormat === 'nested-brackets' ? `${prefix}[${key}]` : `${prefix}.${key}`) : key;
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
parts.push(...this.encodeArray(fullKey, value, arrayFormat));
|
|
23
|
+
}
|
|
24
|
+
else if (typeof value === 'object' && !(value instanceof Date)) {
|
|
25
|
+
if (objectFormat === 'nested-brackets') {
|
|
26
|
+
parts.push(...this.serialize(value, arrayFormat, objectFormat, fullKey));
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
parts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(JSON.stringify(value))}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
parts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return parts;
|
|
37
|
+
}
|
|
38
|
+
encodeArray(key, arr, arrayFormat) {
|
|
39
|
+
const cleanArray = arr.filter((v) => v !== null && v !== undefined).map(String);
|
|
40
|
+
switch (arrayFormat) {
|
|
41
|
+
case 'repeat':
|
|
42
|
+
return cleanArray.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
|
|
43
|
+
case 'brackets':
|
|
44
|
+
return cleanArray.map((v) => `${encodeURIComponent(key)}[]=${encodeURIComponent(v)}`);
|
|
45
|
+
case 'comma':
|
|
46
|
+
return [`${encodeURIComponent(key)}=${encodeURIComponent(cleanArray.join(','))}`];
|
|
47
|
+
case 'json':
|
|
48
|
+
return [`${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(cleanArray))}`];
|
|
49
|
+
default:
|
|
50
|
+
throw new Error(`Unsupported array format: ${arrayFormat}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './QueryBuilder.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './QueryBuilder.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-net/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"!dist/**/*.test.*",
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
],
|
|
109
109
|
"dependencies": {
|
|
110
110
|
"azure-net-tools": "^1.0.8",
|
|
111
|
-
"edges-svelte": "^1.0.
|
|
111
|
+
"edges-svelte": "^1.0.4",
|
|
112
112
|
"edges-svelte-translations": "^0.1.2",
|
|
113
113
|
"ky": "^1.8.1",
|
|
114
114
|
"libphonenumber-js": "^1.12.9"
|