@azure-net/kit 3.1.0 → 3.9.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.
Files changed (71) hide show
  1. package/README.md +2 -2
  2. package/dist/core/delivery/index.d.ts +1 -4
  3. package/dist/core/delivery/index.js +1 -4
  4. package/dist/core/delivery/injectableDependencies/AsyncHelpers.d.ts +28 -30
  5. package/dist/core/delivery/injectableDependencies/AsyncHelpers.js +80 -116
  6. package/dist/core/delivery/injectableDependencies/ErrorHandler.d.ts +3 -21
  7. package/dist/core/delivery/injectableDependencies/ErrorHandler.js +11 -42
  8. package/dist/core/delivery/schema/Schema.d.ts +8 -1
  9. package/dist/core/delivery/schema/Schema.js +12 -3
  10. package/dist/core/delivery.d.ts +5 -0
  11. package/dist/core/delivery.js +5 -0
  12. package/dist/core/errors.d.ts +4 -0
  13. package/dist/core/errors.js +4 -0
  14. package/dist/core/index.d.ts +7 -8
  15. package/dist/core/index.js +7 -8
  16. package/dist/core/infra/datasource/BaseDatasource.d.ts +11 -11
  17. package/dist/core/infra/datasource/BaseDatasource.js +4 -4
  18. package/dist/core/infra/httpService/HttpServiceInstance.d.ts +90 -0
  19. package/dist/core/infra/httpService/HttpServiceInstance.js +340 -0
  20. package/dist/core/infra/httpService/index.d.ts +1 -1
  21. package/dist/core/infra/httpService/index.js +1 -1
  22. package/dist/core/infra/query/QueryBuilder.d.ts +9 -21
  23. package/dist/core/infra/query/QueryBuilder.js +63 -50
  24. package/dist/core/infra/response/BaseResponse.js +3 -2
  25. package/dist/core/infra.d.ts +6 -0
  26. package/dist/core/infra.js +6 -0
  27. package/dist/core/shared/appError/AppError.d.ts +30 -0
  28. package/dist/core/shared/appError/AppError.js +66 -0
  29. package/dist/core/shared/appEventBus/EventBus.d.ts +1 -1
  30. package/dist/core/shared/appEventBus/EventBus.js +3 -3
  31. package/dist/core/shared/boundaryProvider/Provider.d.ts +2 -2
  32. package/dist/core/shared/boundaryProvider/Provider.js +16 -9
  33. package/dist/core/shared/classMirror/ClassMirror.d.ts +7 -2
  34. package/dist/core/shared/classMirror/ClassMirror.js +34 -15
  35. package/dist/core/shared/cookie/UniversalCookie.js +9 -11
  36. package/dist/core/shared/middleware/Middleware.js +12 -8
  37. package/dist/core/svelte/ActiveForm/ActiveForm.svelte.d.ts +10 -6
  38. package/dist/core/svelte/ActiveForm/ActiveForm.svelte.js +47 -41
  39. package/dist/core/svelte/AsyncSignal/AsyncSignal.svelte.d.ts +0 -1
  40. package/dist/core/svelte/AsyncSignal/AsyncSignal.svelte.js +21 -30
  41. package/dist/core/svelte/Query/Query.svelte.d.ts +11 -12
  42. package/dist/core/svelte/Query/Query.svelte.js +43 -55
  43. package/dist/core/ui/onClickOutside/OnClickOutside.js +1 -1
  44. package/dist/core/ui/scrollTo/index.js +2 -2
  45. package/dist/edges/context/index.d.ts +1 -1
  46. package/dist/edges/context/index.js +1 -1
  47. package/dist/edges/dev/index.d.ts +1 -1
  48. package/dist/edges/dev/index.js +1 -1
  49. package/dist/edges/index.d.ts +1 -1
  50. package/dist/edges/index.js +1 -1
  51. package/dist/edges/plugin/index.d.ts +1 -1
  52. package/dist/edges/plugin/index.js +1 -1
  53. package/dist/edges/server/index.d.ts +1 -1
  54. package/dist/edges/server/index.js +1 -1
  55. package/dist/edges/state/index.d.ts +1 -1
  56. package/dist/edges/state/index.js +1 -1
  57. package/dist/i18n/index.d.ts +1 -1
  58. package/dist/i18n/index.js +1 -1
  59. package/dist/tools/index.d.ts +2 -1
  60. package/dist/tools/index.js +2 -1
  61. package/package.json +45 -47
  62. package/dist/core/delivery/serverAction/CreateServerAction.d.ts +0 -15
  63. package/dist/core/delivery/serverAction/CreateServerAction.js +0 -22
  64. package/dist/core/delivery/serverAction/index.d.ts +0 -1
  65. package/dist/core/delivery/serverAction/index.js +0 -1
  66. package/dist/core/infra/httpService/HttpService.d.ts +0 -74
  67. package/dist/core/infra/httpService/HttpService.js +0 -164
  68. package/dist/core/shared/eventBus/EventBus.d.ts +0 -87
  69. package/dist/core/shared/eventBus/EventBus.js +0 -227
  70. package/dist/core/shared/eventBus/index.d.ts +0 -1
  71. package/dist/core/shared/eventBus/index.js +0 -1
@@ -1,72 +1,63 @@
1
- import { ObjectUtil } from 'azure-net-tools';
1
+ import { ObjectUtil } from '@azure-net/tools';
2
2
  import { untrack } from 'svelte';
3
- export const createQuery = (options) => {
4
- const { initial: initialValue, signal: initialSignal, excludeKeys, debounceMs = 0, autoRefresh = true } = options;
3
+ export const createQuery = (initialValue, options) => {
5
4
  const baseInitial = ObjectUtil.deepClone(initialValue);
6
- let signal = initialSignal;
7
5
  let data = $state(ObjectUtil.deepClone(baseInitial));
8
- const resolveKeys = () => {
9
- const keys = Object.keys(baseInitial).slice();
10
- if (excludeKeys?.length) {
11
- return keys.filter((key) => !excludeKeys.includes(key));
12
- }
13
- return keys;
14
- };
15
- const keys = resolveKeys();
16
- let isFirstRun = true;
17
- let debounceTimer = null;
6
+ const includeKeys = options?.onChange?.include ?? [];
7
+ const hasWatch = Boolean(options?.onChange?.handler) && includeKeys.length > 0;
18
8
  const lastValues = new Map();
19
- const scheduleRefresh = () => {
20
- if (!signal)
21
- return;
22
- if (debounceTimer)
23
- clearTimeout(debounceTimer);
24
- if (debounceMs > 0) {
25
- debounceTimer = setTimeout(() => {
26
- untrack(() => {
27
- void signal?.refresh();
28
- });
29
- }, debounceMs);
30
- }
31
- else {
32
- untrack(() => {
33
- void signal?.refresh();
34
- });
35
- }
36
- };
37
- if (autoRefresh) {
9
+ let onChangeController = null;
10
+ let onChangeRunId = 0;
11
+ if (hasWatch) {
12
+ includeKeys.forEach((key) => {
13
+ lastValues.set(key, data[key]);
14
+ });
38
15
  $effect(() => {
39
- let hasChanged = false;
40
- keys.forEach((key) => {
16
+ const changedKeys = [];
17
+ includeKeys.forEach((key) => {
41
18
  const nextValue = data[key];
42
19
  if (!Object.is(lastValues.get(key), nextValue)) {
43
- hasChanged = true;
20
+ changedKeys.push(key);
44
21
  lastValues.set(key, nextValue);
45
22
  }
46
23
  });
47
- if (isFirstRun) {
48
- isFirstRun = false;
24
+ if (!changedKeys.length)
49
25
  return;
50
- }
51
- if (hasChanged) {
52
- scheduleRefresh();
53
- }
26
+ const previousController = onChangeController;
27
+ if (previousController)
28
+ previousController.abort();
29
+ const localController = new AbortController();
30
+ onChangeController = localController;
31
+ const localRunId = ++onChangeRunId;
32
+ const run = async () => {
33
+ await options.onChange.handler({
34
+ changedKeys,
35
+ signal: localController.signal,
36
+ snapshot: ObjectUtil.deepClone(data)
37
+ });
38
+ };
39
+ untrack(() => {
40
+ void run()
41
+ .catch(() => undefined)
42
+ .finally(() => {
43
+ if (onChangeRunId === localRunId && onChangeController === localController) {
44
+ onChangeController = null;
45
+ }
46
+ });
47
+ });
48
+ });
49
+ $effect(() => {
50
+ return () => {
51
+ if (onChangeController)
52
+ onChangeController.abort();
53
+ };
54
54
  });
55
55
  }
56
- const patch = (values) => {
57
- data = { ...data, ...values };
58
- };
59
- const set = (key, value) => {
60
- data = { ...data, [key]: value };
61
- };
62
56
  const reset = () => {
63
57
  data = ObjectUtil.deepClone(baseInitial);
64
58
  };
65
59
  const snapshot = () => ObjectUtil.deepClone(data);
66
60
  const initial = () => ObjectUtil.deepClone(baseInitial);
67
- const attachSignal = (nextSignal) => {
68
- signal = nextSignal;
69
- };
70
61
  return {
71
62
  get data() {
72
63
  return data;
@@ -74,11 +65,8 @@ export const createQuery = (options) => {
74
65
  set data(value) {
75
66
  data = value;
76
67
  },
77
- patch,
78
- set,
79
68
  reset,
80
69
  snapshot,
81
- initial,
82
- attachSignal
70
+ initial
83
71
  };
84
72
  };
@@ -1,4 +1,4 @@
1
- import { EnvironmentUtil } from 'azure-net-tools';
1
+ import { EnvironmentUtil } from '@azure-net/tools';
2
2
  export const onClickOutside = (node, initiator) => {
3
3
  if (EnvironmentUtil.isServer)
4
4
  return { destroy: () => { } };
@@ -1,6 +1,6 @@
1
- import { EnvironmentUtil } from 'azure-net-tools';
1
+ import { BROWSER } from '@azure-net/tools/environment';
2
2
  export const scrollTo = (nodeOrName, offset = 220) => {
3
- if (EnvironmentUtil.isBrowser) {
3
+ if (BROWSER) {
4
4
  setTimeout(() => { }, 1);
5
5
  let element;
6
6
  if (typeof nodeOrName === 'string') {
@@ -1 +1 @@
1
- export { RequestContext } from 'edges-svelte/context';
1
+ export { RequestContext } from '@azure-net/edges/context';
@@ -1 +1 @@
1
- export { RequestContext } from 'edges-svelte/context';
1
+ export { RequestContext } from '@azure-net/edges/context';
@@ -1 +1 @@
1
- export { DevTools } from 'edges-svelte/dev';
1
+ export { DevTools } from '@azure-net/edges/dev';
@@ -1 +1 @@
1
- export { DevTools } from 'edges-svelte/dev';
1
+ export { DevTools } from '@azure-net/edges/dev';
@@ -1 +1 @@
1
- export { createPresenter, createPresenterFactory, createStore, createStoreFactory, batch, transaction, __withEdgesUniversalLoad } from 'edges-svelte';
1
+ export { clearCache, createPresenter, createPresenterFactory, createStore, createStoreFactory, batch, transaction, __withEdgesUniversalLoad } from '@azure-net/edges';
@@ -1 +1 @@
1
- export { createPresenter, createPresenterFactory, createStore, createStoreFactory, batch, transaction, __withEdgesUniversalLoad } from 'edges-svelte';
1
+ export { clearCache, createPresenter, createPresenterFactory, createStore, createStoreFactory, batch, transaction, __withEdgesUniversalLoad } from '@azure-net/edges';
@@ -1 +1 @@
1
- export declare const edgesPlugin: any;
1
+ export declare const edgesPlugin: (options?: import("@azure-net/edges/plugin").EdgesPluginOptions) => import("vite").Plugin;
@@ -1,2 +1,2 @@
1
- import { createEdgesPluginFactory } from 'edges-svelte/plugin';
1
+ import { createEdgesPluginFactory } from '@azure-net/edges/plugin';
2
2
  export const edgesPlugin = createEdgesPluginFactory('@azure-net/kit', '@azure-net/kit/edges/server');
@@ -1 +1 @@
1
- export { edgesHandle, __autoWrapHandle, edgesHandleRaw, __withEdgesUniversalLoad, __withEdgesServerLoad, __withEdgesActions } from 'edges-svelte/server';
1
+ export { edgesHandle, __autoWrapHandle, edgesHandleRaw, __withEdgesUniversalLoad, __withEdgesServerLoad, __withEdgesActions } from '@azure-net/edges/server';
@@ -1 +1 @@
1
- export { edgesHandle, __autoWrapHandle, edgesHandleRaw, __withEdgesUniversalLoad, __withEdgesServerLoad, __withEdgesActions } from 'edges-svelte/server';
1
+ export { edgesHandle, __autoWrapHandle, edgesHandleRaw, __withEdgesUniversalLoad, __withEdgesServerLoad, __withEdgesActions } from '@azure-net/edges/server';
@@ -1 +1 @@
1
- export { createState, createDerivedState, createRawState } from 'edges-svelte/state';
1
+ export { createState, createDerivedState, createRawState } from '@azure-net/edges/state';
@@ -1 +1 @@
1
- export { createState, createDerivedState, createRawState } from 'edges-svelte/state';
1
+ export { createState, createDerivedState, createRawState } from '@azure-net/edges/state';
@@ -1 +1 @@
1
- export { createTranslations, type Path, type Translation } from 'edges-svelte-translations';
1
+ export { createTranslations, type Path, type Translation } from '@azure-net/edges-translations';
@@ -1 +1 @@
1
- export { createTranslations } from 'edges-svelte-translations';
1
+ export { createTranslations } from '@azure-net/edges-translations';
@@ -1 +1,2 @@
1
- export * from 'azure-net-tools';
1
+ export * from '@azure-net/tools';
2
+ export * from '@azure-net/tools/environment';
@@ -1 +1,2 @@
1
- export * from 'azure-net-tools';
1
+ export * from '@azure-net/tools';
2
+ export * from '@azure-net/tools/environment';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "3.1.0",
3
+ "version": "3.9.2",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",
@@ -22,12 +22,16 @@
22
22
  "svelte": "./dist/core/constants/index.js"
23
23
  },
24
24
  "./infra": {
25
- "types": "./dist/core/infra/index.d.ts",
26
- "svelte": "./dist/core/infra/index.js"
25
+ "types": "./dist/core/infra.d.ts",
26
+ "svelte": "./dist/core/infra.js"
27
27
  },
28
- "./schema": {
29
- "types": "./dist/core/delivery/schema/rules/index.d.ts",
30
- "svelte": "./dist/core/delivery/schema/rules/index.js"
28
+ "./delivery": {
29
+ "types": "./dist/core/delivery.d.ts",
30
+ "svelte": "./dist/core/delivery.js"
31
+ },
32
+ "./errors": {
33
+ "types": "./dist/core/errors.d.ts",
34
+ "svelte": "./dist/core/errors.js"
31
35
  },
32
36
  "./edges": {
33
37
  "types": "./dist/edges/index.d.ts",
@@ -64,41 +68,38 @@
64
68
  }
65
69
  },
66
70
  "peerDependencies": {
67
- "svelte": ">=5.0.0",
68
- "@sveltejs/kit": ">=2.16.0"
71
+ "@sveltejs/kit": ">=2.16.0",
72
+ "svelte": ">=5.0.0"
69
73
  },
70
74
  "devDependencies": {
71
- "@eslint/compat": "^1.2.5",
72
- "@eslint/js": "^9.18.0",
73
- "@playwright/test": "^1.49.1",
74
- "@sveltejs/adapter-auto": "^6.0.0",
75
- "@sveltejs/kit": "^2.16.0",
76
- "@sveltejs/package": "^2.0.0",
77
- "@sveltejs/vite-plugin-svelte": "^5.0.0",
78
- "@testing-library/jest-dom": "^6.6.3",
79
- "@testing-library/svelte": "^5.2.4",
80
- "eslint": "^9.18.0",
81
- "eslint-config-prettier": "^10.0.1",
82
- "eslint-plugin-svelte": "^3.0.0",
83
- "globals": "^16.0.0",
84
- "jsdom": "^26.0.0",
85
- "prettier": "^3.4.2",
86
- "prettier-plugin-svelte": "^3.3.3",
87
- "publint": "^0.3.2",
88
- "svelte": "^5.0.0",
89
- "svelte-check": "^4.0.0",
90
- "typescript": "^5.0.0",
91
- "typescript-eslint": "^8.20.0",
92
- "vite": "^6.2.6",
93
- "vitest": "^3.0.0",
94
- "@commitlint/cli": "^19.7.1",
95
- "semantic-release": "^24.2.3",
96
- "@semantic-release/git": "^10.0.1",
97
- "git-cz": "^4.9.0",
98
- "husky": "^9.1.7",
75
+ "@commitlint/cli": "^20.5.0",
76
+ "@eslint/compat": "^2.0.5",
77
+ "@eslint/js": "^10.0.1",
78
+ "@playwright/test": "^1.59.1",
79
+ "@sveltejs/adapter-auto": "^7.0.1",
80
+ "@sveltejs/kit": "^2.57.1",
81
+ "@sveltejs/package": "^2.5.7",
82
+ "@sveltejs/vite-plugin-svelte": "^7.0.0",
83
+ "@testing-library/jest-dom": "^6.9.1",
84
+ "@testing-library/svelte": "^5.3.1",
99
85
  "commander": "^13.1.0",
100
86
  "commitizen": "^4.3.1",
101
- "env-cmd": "^10.1.0"
87
+ "eslint": "^10.2.1",
88
+ "eslint-config-prettier": "^10.1.8",
89
+ "eslint-plugin-svelte": "^3.17.0",
90
+ "git-cz": "^4.9.0",
91
+ "globals": "^17.5.0",
92
+ "husky": "^9.1.7",
93
+ "jsdom": "^29.0.2",
94
+ "prettier": "^3.8.3",
95
+ "prettier-plugin-svelte": "^3.5.1",
96
+ "publint": "^0.3.18",
97
+ "svelte": "^5.55.4",
98
+ "svelte-check": "^4.4.6",
99
+ "typescript": "^5.0.0",
100
+ "typescript-eslint": "^8.58.2",
101
+ "vite": "^8.0.8",
102
+ "vitest": "^4.1.4"
102
103
  },
103
104
  "config": {
104
105
  "commitizen": {
@@ -109,10 +110,9 @@
109
110
  "svelte"
110
111
  ],
111
112
  "dependencies": {
112
- "azure-net-tools": "^1.2.1",
113
- "edges-svelte": "^3.0.3",
114
- "edges-svelte-translations": "^3.0.0",
115
- "ky": "^1.14.3"
113
+ "@azure-net/edges": "^1.2.0",
114
+ "@azure-net/edges-translations": "^1.2.0",
115
+ "@azure-net/tools": "^1.2.0"
116
116
  },
117
117
  "scripts": {
118
118
  "dev": "vite dev",
@@ -122,12 +122,10 @@
122
122
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
123
123
  "format": "prettier --write .",
124
124
  "lint": "prettier --check . && eslint .",
125
- "test:unit": "vitest",
125
+ "test:unit": "vitest --config vitest.config.ts",
126
+ "test:integration": "vitest --config vitest.integration.config.ts --run",
126
127
  "test": "npm run test:unit -- --run && npm run test:e2e",
127
- "test:e2e": "playwright test",
128
- "semantic-release": "env-cmd semantic-release",
129
- "precommit": "pnpm lint && pnpm check",
130
- "commit": "pnpm format && git add . && git-cz && git push",
131
- "release": "pnpm commit && pnpm semantic-release"
128
+ "test:e2e": "playwright test --pass-with-no-tests",
129
+ "commit": "pnpm format && git add . && git-cz && git push"
132
130
  }
133
131
  }
@@ -1,15 +0,0 @@
1
- import { error, fail, redirect, type RequestEvent } from '@sveltejs/kit';
2
- type NoConflict<I, D> = {
3
- [K in keyof I]: K extends keyof D ? never : I[K];
4
- };
5
- type Deps = {
6
- context: RequestEvent;
7
- utils: {
8
- fail: typeof fail;
9
- redirect: typeof redirect;
10
- error: typeof error;
11
- };
12
- };
13
- export declare const createServerAction: <T, I extends Record<string, unknown> = Record<string, unknown>>(factory: (args: Deps & NoConflict<I, Deps>) => T, inject?: I) => (() => T);
14
- export declare const createServerActionFactory: <I extends Record<string, unknown>>(inject: I) => <T>(factory: (args: Deps & NoConflict<I, Deps>) => T) => () => T;
15
- export {};
@@ -1,22 +0,0 @@
1
- import { RequestContext } from '../../../edges/context/index.js';
2
- import { error, fail, redirect } from '@sveltejs/kit';
3
- import { EnvironmentUtil } from 'azure-net-tools';
4
- export const createServerAction = (factory, inject) => {
5
- return () => {
6
- if (EnvironmentUtil.isBrowser) {
7
- throw Error('Do not use actions on client side');
8
- }
9
- const context = RequestContext.current().event;
10
- const deps = {
11
- context,
12
- utils: { fail, redirect, error },
13
- ...inject
14
- };
15
- return factory(deps);
16
- };
17
- };
18
- export const createServerActionFactory = (inject) => {
19
- return function createInjectedServerAction(factory) {
20
- return createServerAction(factory, inject);
21
- };
22
- };
@@ -1 +0,0 @@
1
- export * from './CreateServerAction.js';
@@ -1 +0,0 @@
1
- export * from './CreateServerAction.js';
@@ -1,74 +0,0 @@
1
- import { type Options } from 'ky';
2
- export interface IHttpServiceResponse<T = unknown> {
3
- headers: Record<string, string>;
4
- status: number;
5
- success: boolean;
6
- data: T;
7
- message: string;
8
- }
9
- export interface IHttpServiceError<T = unknown> extends IHttpServiceResponse<T> {
10
- original?: Error;
11
- }
12
- export declare class HttpServiceResponse<T> implements IHttpServiceResponse<T> {
13
- headers: Record<string, string>;
14
- status: number;
15
- success: boolean;
16
- data: T;
17
- message: string;
18
- constructor({ headers, status, success, data, message }: IHttpServiceResponse<T>);
19
- }
20
- export declare class HttpServiceError<T> implements IHttpServiceError<T> {
21
- headers: Record<string, string>;
22
- status: number;
23
- success: boolean;
24
- data: T;
25
- message: string;
26
- original?: Error;
27
- constructor({ headers, status, success, data, message, original }: IHttpServiceError<T>);
28
- }
29
- export interface IHttpServiceOptions extends Options {
30
- responseFormat?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'body';
31
- }
32
- export interface IHttpServiceInstanceResponse<T = unknown> extends Response {
33
- arrayBuffer: () => Promise<ArrayBuffer>;
34
- blob: () => Promise<Blob>;
35
- formData: () => Promise<FormData>;
36
- json: <J = T>() => Promise<J>;
37
- text: () => Promise<string>;
38
- }
39
- export interface IHttpServiceInstance {
40
- get: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
41
- post: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
42
- put: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
43
- delete: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
44
- patch: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
45
- head?: (url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse>;
46
- create: (defaultOptions?: IHttpServiceOptions) => IHttpServiceInstance;
47
- extend?: (defaultOptions: IHttpServiceOptions | ((parentOptions: IHttpServiceOptions) => IHttpServiceOptions)) => IHttpServiceInstance;
48
- readonly stop: typeof stop;
49
- }
50
- export declare class HttpService {
51
- private baseUrl;
52
- private readonly instance;
53
- private readonly requestHandler?;
54
- private readonly errorHandler?;
55
- private readonly globalErrorHandler?;
56
- constructor(opts: {
57
- baseUrl: string;
58
- instance?: IHttpServiceInstance;
59
- baseOptions?: Options;
60
- onRequest?: (options: Options) => void | Promise<void>;
61
- onError?: (err: IHttpServiceError) => HttpServiceError<unknown>;
62
- handleError?: (err: unknown) => Promise<HttpServiceError<unknown>>;
63
- });
64
- setBaseUrl(newBaseUrl: string): void;
65
- private prepareOptions;
66
- private prepareResponse;
67
- post<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
68
- get<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
69
- patch<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
70
- delete<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
71
- put<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
72
- private handleError;
73
- private baseErrorHandler;
74
- }
@@ -1,164 +0,0 @@
1
- import ky, {} from 'ky';
2
- export class HttpServiceResponse {
3
- headers;
4
- status;
5
- success;
6
- data;
7
- message;
8
- constructor({ headers, status, success, data, message }) {
9
- this.headers = headers;
10
- this.status = status;
11
- this.success = success;
12
- this.data = data;
13
- this.message = message;
14
- }
15
- }
16
- export class HttpServiceError {
17
- headers;
18
- status;
19
- success;
20
- data;
21
- message;
22
- original;
23
- constructor({ headers, status, success, data, message, original }) {
24
- this.headers = headers;
25
- this.status = status;
26
- this.success = success;
27
- this.data = data;
28
- this.message = message;
29
- this.original = original;
30
- }
31
- }
32
- export class HttpService {
33
- baseUrl;
34
- instance;
35
- requestHandler;
36
- errorHandler;
37
- globalErrorHandler;
38
- constructor(opts) {
39
- this.baseUrl = opts.baseUrl;
40
- this.instance = opts.instance ? opts.instance.create(opts?.baseOptions) : ky.create(opts?.baseOptions);
41
- this.errorHandler = opts?.onError ?? undefined;
42
- this.requestHandler = opts?.onRequest ?? undefined;
43
- this.globalErrorHandler = opts?.handleError ?? undefined;
44
- }
45
- setBaseUrl(newBaseUrl) {
46
- this.baseUrl = newBaseUrl;
47
- }
48
- async prepareOptions(requestOptions) {
49
- const options = typeof requestOptions === 'object' ? { ...requestOptions } : {};
50
- if (this.requestHandler) {
51
- await this.requestHandler(options);
52
- }
53
- return options;
54
- }
55
- async prepareResponse(request, format = 'json') {
56
- let data;
57
- try {
58
- switch (format) {
59
- case 'blob':
60
- data = (await request.blob());
61
- break;
62
- case 'text':
63
- data = (await request.text());
64
- break;
65
- case 'arrayBuffer':
66
- data = (await request.arrayBuffer());
67
- break;
68
- case 'body':
69
- data = request.body;
70
- break;
71
- case 'json':
72
- data = (await request.json());
73
- break;
74
- default:
75
- data = (await request.json());
76
- break;
77
- }
78
- }
79
- catch {
80
- data = null;
81
- }
82
- return new HttpServiceResponse({
83
- headers: request.headers instanceof Headers ? Object.fromEntries(request.headers.entries()) : {},
84
- status: request.status,
85
- success: request.status < 400,
86
- data,
87
- message: 'Request completed'
88
- });
89
- }
90
- async post(url, options) {
91
- return await this.instance
92
- .post(this.baseUrl + url, await this.prepareOptions(options))
93
- .then((response) => {
94
- return this.prepareResponse(response, options?.responseFormat);
95
- })
96
- .catch(async (error) => {
97
- throw await this.handleError(error);
98
- });
99
- }
100
- async get(url, options) {
101
- return await this.instance
102
- .get(this.baseUrl + url, await this.prepareOptions(options))
103
- .then((response) => {
104
- return this.prepareResponse(response, options?.responseFormat);
105
- })
106
- .catch(async (error) => {
107
- throw await this.handleError(error);
108
- });
109
- }
110
- async patch(url, options) {
111
- return await this.instance
112
- .patch(this.baseUrl + url, await this.prepareOptions(options))
113
- .then((response) => {
114
- return this.prepareResponse(response, options?.responseFormat);
115
- })
116
- .catch(async (error) => {
117
- throw await this.handleError(error);
118
- });
119
- }
120
- async delete(url, options) {
121
- return await this.instance
122
- .delete(this.baseUrl + url, await this.prepareOptions(options))
123
- .then((response) => {
124
- return this.prepareResponse(response, options?.responseFormat);
125
- })
126
- .catch(async (error) => {
127
- throw await this.handleError(error);
128
- });
129
- }
130
- async put(url, options) {
131
- return await this.instance
132
- .put(this.baseUrl + url, await this.prepareOptions(options))
133
- .then((response) => {
134
- return this.prepareResponse(response, options?.responseFormat);
135
- })
136
- .catch(async (error) => {
137
- throw await this.handleError(error);
138
- });
139
- }
140
- async handleError(error) {
141
- return this.globalErrorHandler ? await this.globalErrorHandler(error) : await this.baseErrorHandler(error);
142
- }
143
- async baseErrorHandler(err) {
144
- let headers;
145
- let response;
146
- try {
147
- headers = err?.response?.headers && err?.response?.headers instanceof Headers ? Object.fromEntries(err?.response?.headers.entries()) : {};
148
- response = await err?.response?.json();
149
- }
150
- catch {
151
- headers = {};
152
- response = {};
153
- }
154
- const error = new HttpServiceError({
155
- data: response,
156
- status: err?.response?.status ?? 500,
157
- headers,
158
- success: false,
159
- original: err,
160
- message: 'Request failed'
161
- });
162
- return this.errorHandler ? this.errorHandler(error) : error;
163
- }
164
- }