@azure-net/kit 0.2.5 → 0.3.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.
@@ -1,19 +1,30 @@
1
1
  import { HttpService, HttpServiceResponse } from '../httpService/HttpService.js';
2
- export interface IDatasource {
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 returns a Promise of a response.
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 BaseDatasource implements IDatasource {
15
+ export declare class BaseHttpDatasource implements IHttpDatasource {
14
16
  protected readonly httpClient: HttpService;
15
- constructor(httpClient: HttpService);
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
- export class BaseDatasource {
2
+ import { QueryBuilder } from '../query/index.js';
3
+ export class BaseHttpDatasource {
3
4
  httpClient;
4
- constructor(httpClient) {
5
- this.httpClient = httpClient;
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?: string;
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?: string;
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?: string;
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;
@@ -3,3 +3,4 @@ export * from './response/index.js';
3
3
  export * from './httpService/index.js';
4
4
  export * from './datasource/index.js';
5
5
  export * from './request/index.js';
6
+ export * from './query/index.js';
@@ -3,3 +3,4 @@ export * from './response/index.js';
3
3
  export * from './httpService/index.js';
4
4
  export * from './datasource/index.js';
5
5
  export * from './request/index.js';
6
+ export * from './query/index.js';
@@ -0,0 +1,22 @@
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
+ arrayFormat?: ArrayFormat;
18
+ objectFormat?: ObjectFormat;
19
+ }): string;
20
+ private serialize;
21
+ private encodeArray;
22
+ }
@@ -0,0 +1,52 @@
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
+ return parts.join('&');
13
+ }
14
+ serialize(obj, arrayFormat, objectFormat, prefix = '') {
15
+ const parts = [];
16
+ for (const [key, value] of Object.entries(obj)) {
17
+ if (value === null || value === undefined)
18
+ continue;
19
+ const fullKey = prefix ? (objectFormat === 'nested-brackets' ? `${prefix}[${key}]` : `${prefix}.${key}`) : key;
20
+ if (Array.isArray(value)) {
21
+ parts.push(...this.encodeArray(fullKey, value, arrayFormat));
22
+ }
23
+ else if (typeof value === 'object' && !(value instanceof Date)) {
24
+ if (objectFormat === 'nested-brackets') {
25
+ parts.push(...this.serialize(value, arrayFormat, objectFormat, fullKey));
26
+ }
27
+ else {
28
+ parts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(JSON.stringify(value))}`);
29
+ }
30
+ }
31
+ else {
32
+ parts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`);
33
+ }
34
+ }
35
+ return parts;
36
+ }
37
+ encodeArray(key, arr, arrayFormat) {
38
+ const cleanArray = arr.filter((v) => v !== null && v !== undefined).map(String);
39
+ switch (arrayFormat) {
40
+ case 'repeat':
41
+ return cleanArray.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
42
+ case 'brackets':
43
+ return cleanArray.map((v) => `${encodeURIComponent(key)}[]=${encodeURIComponent(v)}`);
44
+ case 'comma':
45
+ return [`${encodeURIComponent(key)}=${encodeURIComponent(cleanArray.join(','))}`];
46
+ case 'json':
47
+ return [`${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(cleanArray))}`];
48
+ default:
49
+ throw new Error(`Unsupported array format: ${arrayFormat}`);
50
+ }
51
+ }
52
+ }
@@ -0,0 +1 @@
1
+ export * from './QueryBuilder.js';
@@ -0,0 +1 @@
1
+ export * from './QueryBuilder.js';
@@ -1,3 +1,3 @@
1
1
  export declare const onClickOutside: (node: HTMLElement, initiator?: HTMLElement) => {
2
- destroy(): void;
2
+ destroy: () => void;
3
3
  };
@@ -1,4 +1,7 @@
1
+ import { browser } from '$app/environment';
1
2
  export const onClickOutside = (node, initiator) => {
3
+ if (!browser)
4
+ return { destroy: () => { } };
2
5
  const handleClick = (event) => {
3
6
  if (event.target instanceof HTMLElement) {
4
7
  if (event.target === node ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
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.3",
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"