@diphyx/harlemify 3.0.0 → 4.0.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.
Files changed (59) hide show
  1. package/README.md +51 -32
  2. package/dist/module.d.mts +24 -13
  3. package/dist/module.d.ts +24 -13
  4. package/dist/module.json +1 -1
  5. package/dist/module.mjs +8 -10
  6. package/dist/runtime/composables/action.d.ts +4 -0
  7. package/dist/runtime/composables/action.js +8 -0
  8. package/dist/runtime/config.d.ts +10 -0
  9. package/dist/runtime/config.js +7 -0
  10. package/dist/runtime/core/layers/action.d.ts +4 -0
  11. package/dist/runtime/core/layers/action.js +102 -0
  12. package/dist/runtime/core/layers/model.d.ts +3 -0
  13. package/dist/runtime/core/layers/model.js +31 -0
  14. package/dist/runtime/core/layers/shape.d.ts +45 -0
  15. package/dist/runtime/core/layers/shape.js +56 -0
  16. package/dist/runtime/core/layers/view.d.ts +4 -0
  17. package/dist/runtime/core/layers/view.js +21 -0
  18. package/dist/runtime/core/store.d.ts +20 -66
  19. package/dist/runtime/core/store.js +46 -574
  20. package/dist/runtime/core/types/action.d.ts +163 -0
  21. package/dist/runtime/core/types/action.js +39 -0
  22. package/dist/runtime/core/types/model.d.ts +70 -0
  23. package/dist/runtime/core/types/model.js +5 -0
  24. package/dist/runtime/core/types/shape.d.ts +46 -0
  25. package/dist/runtime/core/types/shape.js +0 -0
  26. package/dist/runtime/core/types/view.d.ts +29 -0
  27. package/dist/runtime/core/types/view.js +0 -0
  28. package/dist/runtime/core/utils/action.d.ts +4 -0
  29. package/dist/runtime/core/utils/action.js +294 -0
  30. package/dist/runtime/core/utils/model.d.ts +12 -0
  31. package/dist/runtime/core/utils/model.js +227 -0
  32. package/dist/runtime/core/utils/shape.d.ts +3 -0
  33. package/dist/runtime/core/utils/shape.js +29 -0
  34. package/dist/runtime/core/utils/view.d.ts +5 -0
  35. package/dist/runtime/core/utils/view.js +19 -0
  36. package/dist/runtime/index.d.ts +8 -16
  37. package/dist/runtime/index.js +11 -7
  38. package/dist/runtime/plugin.js +2 -5
  39. package/package.json +2 -1
  40. package/dist/runtime/composables/use.d.ts +0 -22
  41. package/dist/runtime/composables/use.js +0 -14
  42. package/dist/runtime/core/api.d.ts +0 -37
  43. package/dist/runtime/core/api.js +0 -56
  44. package/dist/runtime/shared.d.ts +0 -9
  45. package/dist/runtime/shared.js +0 -3
  46. package/dist/runtime/utils/adapter.d.ts +0 -24
  47. package/dist/runtime/utils/adapter.js +0 -35
  48. package/dist/runtime/utils/cache.d.ts +0 -10
  49. package/dist/runtime/utils/cache.js +0 -26
  50. package/dist/runtime/utils/endpoint.d.ts +0 -38
  51. package/dist/runtime/utils/endpoint.js +0 -56
  52. package/dist/runtime/utils/errors.d.ts +0 -22
  53. package/dist/runtime/utils/errors.js +0 -33
  54. package/dist/runtime/utils/memory.d.ts +0 -40
  55. package/dist/runtime/utils/memory.js +0 -87
  56. package/dist/runtime/utils/schema.d.ts +0 -23
  57. package/dist/runtime/utils/schema.js +0 -58
  58. package/dist/runtime/utils/transform.d.ts +0 -6
  59. package/dist/runtime/utils/transform.js +0 -22
@@ -0,0 +1,227 @@
1
+ import { defu } from "defu";
2
+ import { resolveShape } from "./shape.js";
3
+ import {
4
+ ModelKind
5
+ } from "../types/model.js";
6
+ import { ActionOneMode, ActionManyMode, AUTO } from "../types/action.js";
7
+ export function initializeState(model) {
8
+ const state = {};
9
+ for (const [key, definition] of Object.entries(model)) {
10
+ if (definition.kind === ModelKind.OBJECT) {
11
+ state[key] = definition.options?.default ?? null;
12
+ } else {
13
+ state[key] = definition.options?.default ?? [];
14
+ }
15
+ }
16
+ return state;
17
+ }
18
+ function getIdentifier(definition) {
19
+ if (definition.options?.identifier) {
20
+ return definition.options.identifier;
21
+ }
22
+ const { identifier } = resolveShape(definition.shape);
23
+ if (identifier) {
24
+ return identifier;
25
+ }
26
+ return "id";
27
+ }
28
+ function createOneMutations(source, definition, key) {
29
+ const setOperation = source.mutation(`${key}:set`, (state, value) => {
30
+ state[key] = value;
31
+ });
32
+ const resetOperation = source.mutation(`${key}:reset`, (state) => {
33
+ state[key] = definition.options?.default ?? null;
34
+ });
35
+ const patchOperation = source.mutation(`${key}:patch`, (state, payload) => {
36
+ if (state[key] === null) {
37
+ return;
38
+ }
39
+ if (payload.options?.deep) {
40
+ state[key] = defu(payload.value, state[key]);
41
+ return;
42
+ }
43
+ state[key] = {
44
+ ...state[key],
45
+ ...payload.value
46
+ };
47
+ });
48
+ function set(value) {
49
+ definition.logger?.debug("Model mutation", {
50
+ model: key,
51
+ mutation: "set"
52
+ });
53
+ setOperation(value);
54
+ }
55
+ function reset() {
56
+ definition.logger?.debug("Model mutation", {
57
+ model: key,
58
+ mutation: "reset"
59
+ });
60
+ resetOperation();
61
+ }
62
+ function patch(value, options) {
63
+ definition.logger?.debug("Model mutation", {
64
+ model: key,
65
+ mutation: "patch"
66
+ });
67
+ patchOperation({
68
+ value,
69
+ options
70
+ });
71
+ }
72
+ return {
73
+ set,
74
+ reset,
75
+ patch
76
+ };
77
+ }
78
+ function createManyMutations(source, definition, key) {
79
+ const identifier = getIdentifier(definition);
80
+ const setOperation = source.mutation(`${key}:set`, (state, value) => {
81
+ state[key] = value;
82
+ });
83
+ const resetOperation = source.mutation(`${key}:reset`, (state) => {
84
+ state[key] = definition.options?.default ?? [];
85
+ });
86
+ const patchOperation = source.mutation(
87
+ `${key}:patch`,
88
+ (state, payload) => {
89
+ const items = Array.isArray(payload.value) ? payload.value : [payload.value];
90
+ const by = payload.options?.by ?? identifier;
91
+ state[key] = state[key].map((item) => {
92
+ const found = items.find((p) => {
93
+ return p[by] === item[by];
94
+ });
95
+ if (!found) {
96
+ return item;
97
+ }
98
+ if (payload.options?.deep) {
99
+ return defu(found, item);
100
+ }
101
+ return {
102
+ ...item,
103
+ ...found
104
+ };
105
+ });
106
+ }
107
+ );
108
+ const removeOperation = source.mutation(`${key}:remove`, (state, payload) => {
109
+ const items = Array.isArray(payload.value) ? payload.value : [payload.value];
110
+ const by = payload.options?.by ?? identifier;
111
+ const ids = new Set(
112
+ items.map((item) => {
113
+ return item[by];
114
+ })
115
+ );
116
+ state[key] = state[key].filter((item) => {
117
+ return !ids.has(item[by]);
118
+ });
119
+ });
120
+ const addOperation = source.mutation(`${key}:add`, (state, payload) => {
121
+ let items = Array.isArray(payload.value) ? payload.value : [payload.value];
122
+ if (payload.options?.unique) {
123
+ const by = payload.options.by ?? identifier;
124
+ const existingIds = new Set(
125
+ state[key].map((item) => {
126
+ return item[by];
127
+ })
128
+ );
129
+ items = items.filter((item) => {
130
+ return !existingIds.has(item[by]);
131
+ });
132
+ }
133
+ if (payload.options?.prepend) {
134
+ state[key] = [...items, ...state[key]];
135
+ return;
136
+ }
137
+ state[key] = [...state[key], ...items];
138
+ });
139
+ function set(value) {
140
+ definition.logger?.debug("Model mutation", {
141
+ model: key,
142
+ mutation: "set"
143
+ });
144
+ setOperation(value);
145
+ }
146
+ function reset() {
147
+ definition.logger?.debug("Model mutation", {
148
+ model: key,
149
+ mutation: "reset"
150
+ });
151
+ resetOperation();
152
+ }
153
+ function patch(value, options) {
154
+ definition.logger?.debug("Model mutation", {
155
+ model: key,
156
+ mutation: "patch"
157
+ });
158
+ patchOperation({
159
+ value,
160
+ options
161
+ });
162
+ }
163
+ function remove(value, options) {
164
+ definition.logger?.debug("Model mutation", {
165
+ model: key,
166
+ mutation: "remove"
167
+ });
168
+ removeOperation({
169
+ value,
170
+ options
171
+ });
172
+ }
173
+ function add(value, options) {
174
+ definition.logger?.debug("Model mutation", {
175
+ model: key,
176
+ mutation: "add"
177
+ });
178
+ addOperation({
179
+ value,
180
+ options
181
+ });
182
+ }
183
+ return {
184
+ set,
185
+ reset,
186
+ patch,
187
+ remove,
188
+ add
189
+ };
190
+ }
191
+ export function createMutations(source, model) {
192
+ const mutations = {};
193
+ for (const [key, definition] of Object.entries(model)) {
194
+ definition.logger?.debug("Model registered", {
195
+ model: key,
196
+ kind: definition.kind
197
+ });
198
+ if (definition.kind === ModelKind.OBJECT) {
199
+ mutations[key] = createOneMutations(source, definition, key);
200
+ } else {
201
+ mutations[key] = createManyMutations(source, definition, key);
202
+ }
203
+ }
204
+ return mutations;
205
+ }
206
+ export function executeCommit(definition, mutations, result) {
207
+ const handler = mutations[definition.model][definition.mode];
208
+ switch (definition.mode) {
209
+ case ActionOneMode.RESET:
210
+ case ActionManyMode.RESET: {
211
+ handler();
212
+ break;
213
+ }
214
+ default: {
215
+ handler(
216
+ definition.value === AUTO || definition.value === void 0 ? result : definition.value,
217
+ definition.options
218
+ );
219
+ }
220
+ }
221
+ }
222
+ export function createCommitter(mutations) {
223
+ function committer(model, mode, value, options) {
224
+ executeCommit({ model, mode, value, options }, mutations);
225
+ }
226
+ return committer;
227
+ }
@@ -0,0 +1,3 @@
1
+ import type { z } from "zod";
2
+ import type { ShapeMeta } from "../types/shape.js";
3
+ export declare function resolveShape(shape: z.ZodObject<z.ZodRawShape>): ShapeMeta;
@@ -0,0 +1,29 @@
1
+ export function resolveShape(shape) {
2
+ const meta = {
3
+ identifier: void 0,
4
+ defaults: {},
5
+ fields: []
6
+ };
7
+ for (const [key, field] of Object.entries(shape.shape)) {
8
+ meta.fields.push(key);
9
+ const fieldDefinition = field.def;
10
+ if (fieldDefinition?.meta?.identifier) {
11
+ meta.identifier = key;
12
+ }
13
+ if (fieldDefinition?.defaultValue !== void 0) {
14
+ if (typeof fieldDefinition.defaultValue === "function") {
15
+ meta.defaults[key] = fieldDefinition.defaultValue();
16
+ continue;
17
+ }
18
+ meta.defaults[key] = fieldDefinition.defaultValue;
19
+ }
20
+ }
21
+ if (!meta.identifier) {
22
+ if (meta.fields.includes("id")) {
23
+ meta.identifier = "id";
24
+ } else if (meta.fields.includes("_id")) {
25
+ meta.identifier = "_id";
26
+ }
27
+ }
28
+ return meta;
29
+ }
@@ -0,0 +1,5 @@
1
+ import type { Store as SourceStore, BaseState } from "@harlem/core";
2
+ import type { StoreView } from "../store.js";
3
+ import type { Model } from "../types/model.js";
4
+ import type { ViewDefinitions } from "../types/view.js";
5
+ export declare function createView<M extends Model, VD extends ViewDefinitions<M>>(source: SourceStore<BaseState>, definitions: VD): StoreView<M, VD>;
@@ -0,0 +1,19 @@
1
+ export function createView(source, definitions) {
2
+ const view = {};
3
+ for (const [key, definition] of Object.entries(definitions)) {
4
+ definition.logger?.debug("View registered", {
5
+ view: key,
6
+ sources: definition.sources
7
+ });
8
+ view[key] = source.getter(key, (state) => {
9
+ const values = definition.sources.map((sourceKey) => {
10
+ return state[sourceKey];
11
+ });
12
+ if (definition.resolver) {
13
+ return definition.resolver(...values);
14
+ }
15
+ return values[0];
16
+ });
17
+ }
18
+ return view;
19
+ }
@@ -1,17 +1,9 @@
1
- export { defineApiAdapter } from "./utils/adapter.js";
2
- export { Endpoint, EndpointMethod, EndpointStatus, resolveEndpointUrl } from "./utils/endpoint.js";
3
- export { ApiErrorSource, ApiError, ApiRequestError, ApiResponseError } from "./utils/errors.js";
4
- export { Memory, createMemoryBuilder } from "./utils/memory.js";
5
- export { getMeta, resolveSchema } from "./utils/schema.js";
6
- export { createApi } from "./core/api.js";
7
1
  export { createStore } from "./core/store.js";
8
- export { useStoreAlias } from "./composables/use.js";
9
- export type { ApiAdapter, ApiAdapterRequest, ApiAdapterResponse, ApiFetchAdapterOptions, DefineApiAdapter, } from "./utils/adapter.js";
10
- export type { EndpointBuilder, EndpointChain, EndpointDefinition, EndpointUrl } from "./utils/endpoint.js";
11
- export type { ApiErrorOptions } from "./utils/errors.js";
12
- export type { MemoryBuilder, MemoryDefinition, MemoryMutation, MemoryTarget, EditOptions, AddOptions, UnitMemoryBuilder, UnitsMemoryBuilder, } from "./utils/memory.js";
13
- export type { SchemaMeta } from "./utils/schema.js";
14
- export type { Api, ApiOptions, ApiRequestBody, ApiRequestHeader, ApiRequestOptions, ApiRequestQuery, EndpointMethodOptions, } from "./core/api.js";
15
- export type { ActionDefinition, ActionFunction, ActionOptions, ActionStatus, ActionsConfig, Store, StoreActions, StoreHooks, StoreMemory, StoreMonitor, StoreOptions, } from "./core/store.js";
16
- export type { StoreAlias } from "./composables/use.js";
17
- export type { SharedConfig } from "./shared.js";
2
+ export type { Store, StoreConfig } from "./core/store.js";
3
+ export { shape } from "./core/layers/shape.js";
4
+ export type { ShapeInfer } from "./core/types/shape.js";
5
+ export { ModelKind } from "./core/types/model.js";
6
+ export { AUTO, ActionOneMode, ActionManyMode, ActionStatus, ActionConcurrent, ActionApiMethod, } from "./core/types/action.js";
7
+ export type { Action, ActionCallPayload, ActionApiShortcutDefinition, ActionError, ActionApiError, ActionHandleError, ActionCommitError, ActionConcurrentError, } from "./core/types/action.js";
8
+ export { useIsolatedActionStatus, useIsolatedActionError } from "./composables/action.js";
9
+ export type { RuntimeConfig } from "./config.js";
@@ -1,8 +1,12 @@
1
- export { defineApiAdapter } from "./utils/adapter.js";
2
- export { Endpoint, EndpointMethod, EndpointStatus, resolveEndpointUrl } from "./utils/endpoint.js";
3
- export { ApiErrorSource, ApiError, ApiRequestError, ApiResponseError } from "./utils/errors.js";
4
- export { Memory, createMemoryBuilder } from "./utils/memory.js";
5
- export { getMeta, resolveSchema } from "./utils/schema.js";
6
- export { createApi } from "./core/api.js";
7
1
  export { createStore } from "./core/store.js";
8
- export { useStoreAlias } from "./composables/use.js";
2
+ export { shape } from "./core/layers/shape.js";
3
+ export { ModelKind } from "./core/types/model.js";
4
+ export {
5
+ AUTO,
6
+ ActionOneMode,
7
+ ActionManyMode,
8
+ ActionStatus,
9
+ ActionConcurrent,
10
+ ActionApiMethod
11
+ } from "./core/types/action.js";
12
+ export { useIsolatedActionStatus, useIsolatedActionError } from "./composables/action.js";
@@ -1,15 +1,12 @@
1
- import { defineNuxtPlugin, useHead } from "#imports";
2
1
  import { createVuePlugin } from "@harlem/core";
3
2
  import { createServerSSRPlugin, createClientSSRPlugin, getBridgingScript } from "@harlem/plugin-ssr";
4
- import config from "#build/harlemify.config";
5
- import { sharedConfig } from "./shared.js";
3
+ import { defineNuxtPlugin, useHead } from "#imports";
6
4
  export default defineNuxtPlugin((nuxtApp) => {
7
- sharedConfig.api = config.api;
8
5
  const plugins = [];
9
6
  if (import.meta.server) {
10
7
  plugins.push(createServerSSRPlugin());
11
8
  }
12
- if (import.meta.client && window.__harlemState) {
9
+ if (import.meta.client && window["__harlemState"]) {
13
10
  plugins.push(createClientSSRPlugin());
14
11
  }
15
12
  const harlem = createVuePlugin({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diphyx/harlemify",
3
- "version": "3.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "API state management for Nuxt powered by Harlem",
5
5
  "keywords": [
6
6
  "nuxt",
@@ -44,6 +44,7 @@
44
44
  "@harlem/core": "^3.0.0",
45
45
  "@harlem/plugin-ssr": "^3.0.0",
46
46
  "@nuxt/kit": "^3.14.0",
47
+ "consola": "^3.0.0",
47
48
  "defu": "^6.0.0",
48
49
  "zod": "^4.0.0"
49
50
  },
@@ -1,22 +0,0 @@
1
- import type { ComputedRef } from "vue";
2
- import type { ActionFunction, ActionStatus, ActionsConfig, Store, StoreMemory } from "../core/store.js";
3
- import type { Capitalize, Pluralize } from "../utils/transform.js";
4
- type MemoryState<E extends string, T> = {
5
- [P in E]: ComputedRef<T | null>;
6
- } & {
7
- [P in Pluralize<E>]: ComputedRef<T[]>;
8
- };
9
- type AliasActions<E extends string, A extends ActionsConfig<S>, S> = {
10
- [K in keyof A as `${K & string}${Capitalize<E>}`]: ActionFunction<S>;
11
- };
12
- type AliasMemory<E extends string, S, I extends keyof S> = {
13
- [K in `${E}Memory`]: StoreMemory<S, I>;
14
- };
15
- type AliasMonitor<E extends string, A extends ActionsConfig<any>> = {
16
- [K in `${E}Monitor`]: {
17
- [ActionName in keyof A]: ActionStatus;
18
- };
19
- };
20
- export type StoreAlias<E extends string, S, I extends keyof S, A extends ActionsConfig<S>> = MemoryState<E, S> & AliasActions<E, A, S> & AliasMemory<E, S, I> & AliasMonitor<E, A>;
21
- export declare function useStoreAlias<E extends string, S, I extends keyof S, A extends ActionsConfig<S>>(store: Store<E, S, I, A>): StoreAlias<E, S, I, A>;
22
- export {};
@@ -1,14 +0,0 @@
1
- import { capitalize } from "../utils/transform.js";
2
- export function useStoreAlias(store) {
3
- const capitalizedEntity = capitalize(store.alias.unit);
4
- const output = {
5
- [store.alias.unit]: store.unit,
6
- [store.alias.units]: store.units,
7
- [`${store.alias.unit}Memory`]: store.memory,
8
- [`${store.alias.unit}Monitor`]: store.monitor
9
- };
10
- for (const actionName in store.action) {
11
- output[`${actionName}${capitalizedEntity}`] = store.action[actionName];
12
- }
13
- return output;
14
- }
@@ -1,37 +0,0 @@
1
- import type { MaybeRefOrGetter } from "vue";
2
- import { EndpointMethod } from "../utils/endpoint.js";
3
- import type { ApiAdapter } from "../utils/adapter.js";
4
- export type ApiRequestHeader = MaybeRefOrGetter<Record<string, unknown>>;
5
- export type ApiRequestQuery = MaybeRefOrGetter<Record<string, unknown>>;
6
- export type ApiRequestBody = MaybeRefOrGetter<string | number | ArrayBuffer | FormData | Blob | Record<string, any>>;
7
- export interface ApiRequestOptions<A extends EndpointMethod = EndpointMethod, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery, B extends ApiRequestBody = ApiRequestBody> {
8
- action?: A;
9
- headers?: H;
10
- query?: Q;
11
- body?: B;
12
- signal?: AbortSignal;
13
- }
14
- export interface ApiOptions {
15
- headers?: ApiRequestHeader;
16
- query?: ApiRequestQuery;
17
- adapter?: ApiAdapter<any>;
18
- }
19
- export type EndpointMethodOptions<A extends EndpointMethod, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery, B extends ApiRequestBody = ApiRequestBody> = Omit<ApiRequestOptions<A, H, Q, B>, "action">;
20
- export interface Api {
21
- get: <T, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery>(url: string, options?: EndpointMethodOptions<EndpointMethod.GET, H, Q, never> & {
22
- adapter?: ApiAdapter<T>;
23
- }) => Promise<T>;
24
- post: <T, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery, B extends ApiRequestBody = ApiRequestBody>(url: string, options?: EndpointMethodOptions<EndpointMethod.POST, H, Q, B> & {
25
- adapter?: ApiAdapter<T>;
26
- }) => Promise<T>;
27
- put: <T, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery, B extends ApiRequestBody = ApiRequestBody>(url: string, options?: EndpointMethodOptions<EndpointMethod.PUT, H, Q, B> & {
28
- adapter?: ApiAdapter<T>;
29
- }) => Promise<T>;
30
- patch: <T, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery, B extends ApiRequestBody = ApiRequestBody>(url: string, options?: EndpointMethodOptions<EndpointMethod.PATCH, H, Q, B> & {
31
- adapter?: ApiAdapter<T>;
32
- }) => Promise<T>;
33
- del: <T, H extends ApiRequestHeader = ApiRequestHeader, Q extends ApiRequestQuery = ApiRequestQuery>(url: string, options?: EndpointMethodOptions<EndpointMethod.DELETE, H, Q, never> & {
34
- adapter?: ApiAdapter<T>;
35
- }) => Promise<T>;
36
- }
37
- export declare function createApi(options?: ApiOptions): Api;
@@ -1,56 +0,0 @@
1
- import { toValue } from "vue";
2
- import { defineApiAdapter } from "../utils/adapter.js";
3
- import { EndpointMethod } from "../utils/endpoint.js";
4
- export function createApi(options) {
5
- const defaultAdapter = options?.adapter ?? defineApiAdapter();
6
- async function request(url, requestOptions) {
7
- const adapter = requestOptions?.adapter ?? defaultAdapter;
8
- const adapterRequest = {
9
- method: requestOptions?.action ?? EndpointMethod.GET,
10
- url,
11
- body: toValue(requestOptions?.body),
12
- query: Object.assign({}, toValue(options?.query), toValue(requestOptions?.query)),
13
- headers: Object.assign({}, toValue(options?.headers), toValue(requestOptions?.headers)),
14
- signal: requestOptions?.signal
15
- };
16
- const response = await adapter(adapterRequest);
17
- return response.data;
18
- }
19
- async function get(url, options2) {
20
- return request(url, {
21
- ...options2,
22
- action: EndpointMethod.GET
23
- });
24
- }
25
- async function post(url, options2) {
26
- return request(url, {
27
- ...options2,
28
- action: EndpointMethod.POST
29
- });
30
- }
31
- async function put(url, options2) {
32
- return request(url, {
33
- ...options2,
34
- action: EndpointMethod.PUT
35
- });
36
- }
37
- async function patch(url, options2) {
38
- return request(url, {
39
- ...options2,
40
- action: EndpointMethod.PATCH
41
- });
42
- }
43
- async function del(url, options2) {
44
- return request(url, {
45
- ...options2,
46
- action: EndpointMethod.DELETE
47
- });
48
- }
49
- return {
50
- get,
51
- post,
52
- put,
53
- patch,
54
- del
55
- };
56
- }
@@ -1,9 +0,0 @@
1
- import type { ApiFetchAdapterOptions } from "./utils/adapter.js";
2
- export type SharedConfig = {
3
- api?: {
4
- headers?: Record<string, string>;
5
- query?: Record<string, unknown>;
6
- adapter?: ApiFetchAdapterOptions;
7
- };
8
- };
9
- export declare const sharedConfig: SharedConfig;
@@ -1,3 +0,0 @@
1
- export const sharedConfig = {
2
- api: {}
3
- };
@@ -1,24 +0,0 @@
1
- import type { EndpointMethod } from "./endpoint.js";
2
- export interface ApiAdapterRequest {
3
- method: EndpointMethod;
4
- url: string;
5
- body?: unknown;
6
- query?: Record<string, unknown>;
7
- headers?: Record<string, string>;
8
- signal?: AbortSignal;
9
- }
10
- export interface ApiAdapterResponse<T = unknown> {
11
- data: T;
12
- status?: number;
13
- }
14
- export type ApiAdapter<T = unknown> = (request: ApiAdapterRequest) => Promise<ApiAdapterResponse<T>>;
15
- export type DefineApiAdapter<T = unknown, O = unknown> = (options?: O) => ApiAdapter<T>;
16
- export interface ApiFetchAdapterOptions {
17
- baseURL?: string;
18
- timeout?: number;
19
- retry?: number | false;
20
- retryDelay?: number;
21
- retryStatusCodes?: number[];
22
- responseType?: "json" | "text" | "blob" | "arrayBuffer";
23
- }
24
- export declare function defineApiAdapter<T = unknown>(options?: ApiFetchAdapterOptions): ApiAdapter<T>;
@@ -1,35 +0,0 @@
1
- import { ApiRequestError, ApiResponseError } from "./errors.js";
2
- export function defineApiAdapter(options) {
3
- return async (request) => {
4
- const data = await $fetch(request.url, {
5
- baseURL: options?.baseURL,
6
- method: request.method,
7
- headers: request.headers,
8
- query: request.query,
9
- body: request.body,
10
- timeout: options?.timeout,
11
- signal: request.signal,
12
- retry: options?.retry,
13
- retryDelay: options?.retryDelay,
14
- retryStatusCodes: options?.retryStatusCodes,
15
- responseType: options?.responseType,
16
- onRequestError({ request: request2, options: options2, error }) {
17
- throw new ApiRequestError({
18
- method: options2.method,
19
- url: request2.toString(),
20
- message: error?.message
21
- });
22
- },
23
- onResponseError({ request: request2, options: options2, error }) {
24
- throw new ApiResponseError({
25
- method: options2.method,
26
- url: request2.toString(),
27
- message: error?.message
28
- });
29
- }
30
- });
31
- return {
32
- data
33
- };
34
- };
35
- }
@@ -1,10 +0,0 @@
1
- export interface Cache<K, V> {
2
- get(key: K): V | undefined;
3
- set(key: K, value: V): void;
4
- delete(key: K): boolean;
5
- clear(): void;
6
- size(): number;
7
- has(key: K): boolean;
8
- entries(): IterableIterator<[K, V]>;
9
- }
10
- export declare function createCache<K, V>(): Cache<K, V>;
@@ -1,26 +0,0 @@
1
- export function createCache() {
2
- const map = /* @__PURE__ */ new Map();
3
- return {
4
- get(key) {
5
- return map.get(key);
6
- },
7
- set(key, value) {
8
- map.set(key, value);
9
- },
10
- delete(key) {
11
- return map.delete(key);
12
- },
13
- clear() {
14
- map.clear();
15
- },
16
- size() {
17
- return map.size;
18
- },
19
- has(key) {
20
- return map.has(key);
21
- },
22
- entries() {
23
- return map.entries();
24
- }
25
- };
26
- }
@@ -1,38 +0,0 @@
1
- import type { ApiAdapter } from "./adapter.js";
2
- import type { Capitalize } from "./transform.js";
3
- export declare enum EndpointMethod {
4
- GET = "get",
5
- POST = "post",
6
- PUT = "put",
7
- PATCH = "patch",
8
- DELETE = "delete"
9
- }
10
- export declare enum EndpointStatus {
11
- IDLE = "idle",
12
- PENDING = "pending",
13
- SUCCESS = "success",
14
- FAILED = "failed"
15
- }
16
- export type EndpointUrl<S> = string | ((params: Partial<S>) => string);
17
- export interface EndpointDefinition<S = Record<string, unknown>> {
18
- readonly method: EndpointMethod;
19
- readonly url: EndpointUrl<S>;
20
- readonly adapter?: ApiAdapter<any>;
21
- }
22
- export interface EndpointChain<S> {
23
- readonly method: EndpointMethod;
24
- readonly url: EndpointUrl<S>;
25
- readonly adapter?: ApiAdapter<any>;
26
- withAdapter(adapter: ApiAdapter<any>): EndpointDefinition<S>;
27
- }
28
- export interface EndpointBuilder {
29
- get<S = Record<string, unknown>>(url: EndpointUrl<S>): EndpointChain<S>;
30
- post<S = Record<string, unknown>>(url: EndpointUrl<S>): EndpointChain<S>;
31
- put<S = Record<string, unknown>>(url: EndpointUrl<S>): EndpointChain<S>;
32
- patch<S = Record<string, unknown>>(url: EndpointUrl<S>): EndpointChain<S>;
33
- delete<S = Record<string, unknown>>(url: EndpointUrl<S>): EndpointChain<S>;
34
- }
35
- export declare const Endpoint: EndpointBuilder;
36
- export type EndpointStatusFlag<S extends EndpointStatus = EndpointStatus> = `Is${Capitalize<S>}`;
37
- export declare function makeEndpointStatusFlag<S extends EndpointStatus>(status: S): EndpointStatusFlag<S>;
38
- export declare function resolveEndpointUrl<S>(endpoint: EndpointDefinition<S>, params?: Partial<S>): string;