@brickflow/http 0.0.14 → 0.0.16
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 +272 -288
- package/dist/create-get.d.ts +12 -0
- package/dist/create-get.d.ts.map +1 -0
- package/dist/http.d.ts +46 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +171 -0
- package/dist/index.mjs.map +1 -0
- package/dist/nuxt-DAmJOX58.js +233 -0
- package/dist/nuxt-DAmJOX58.js.map +1 -0
- package/dist/nuxt.d.ts +55 -0
- package/dist/nuxt.d.ts.map +1 -0
- package/dist/nuxt.mjs +2 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +30 -18
- package/src/app.d.ts +11 -0
- package/src/create-get.ts +43 -0
- package/src/http.ts +286 -0
- package/src/index.ts +3 -0
- package/src/nuxt.ts +355 -0
- package/src/utils.ts +50 -0
- package/dist/module.d.mts +0 -73
- package/dist/module.json +0 -12
- package/dist/module.mjs +0 -59
- package/dist/runtime/composables/useHttp.d.ts +0 -27
- package/dist/runtime/composables/useHttp.js +0 -197
- package/dist/runtime/http/client.d.ts +0 -3
- package/dist/runtime/http/client.js +0 -217
- package/dist/runtime/plugin.d.ts +0 -7
- package/dist/runtime/plugin.js +0 -56
- package/dist/runtime/types.d.ts +0 -21
- package/dist/runtime/utils/helpers.d.ts +0 -5
- package/dist/runtime/utils/helpers.js +0 -14
- package/dist/runtime/utils/index.d.ts +0 -6
- package/dist/runtime/utils/index.js +0 -5
- package/dist/runtime/utils/indexeddb.d.ts +0 -14
- package/dist/runtime/utils/indexeddb.js +0 -222
- package/dist/runtime/utils/middleware.d.ts +0 -8
- package/dist/runtime/utils/middleware.js +0 -20
- package/dist/runtime/utils/shared.d.ts +0 -83
- package/dist/runtime/utils/shared.js +0 -50
- package/dist/runtime/utils/typed.d.ts +0 -46
- package/dist/runtime/utils/typed.js +0 -9
- package/dist/types.d.mts +0 -11
package/src/nuxt.ts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { onScopeDispose, shallowReactive, shallowRef } from 'vue'
|
|
2
|
+
|
|
3
|
+
import { useLazyAsyncData, useState } from '#app'
|
|
4
|
+
|
|
5
|
+
import type { GetConfig, HttpClient, HttpErrorGuard, HttpKey, HttpParam, HttpResponseData } from './http'
|
|
6
|
+
|
|
7
|
+
import { createURL, hashData } from './utils'
|
|
8
|
+
|
|
9
|
+
const DAY = 1000 * 60 * 60 * 24
|
|
10
|
+
const DEFAULT_CHANNEL_NAME = 'http-tab-sync'
|
|
11
|
+
const DEFAULT_TTL = DAY * 7
|
|
12
|
+
export interface CreateUseHttpDependencies {
|
|
13
|
+
channelName?: string
|
|
14
|
+
getCache: () => null | UseHttpCache
|
|
15
|
+
getHttpClient: () => HttpClient
|
|
16
|
+
isDev?: () => boolean
|
|
17
|
+
isError?: HttpErrorGuard<unknown>
|
|
18
|
+
ttl?: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type HttpError = HttpErrorMap[keyof HttpErrorMap]
|
|
22
|
+
export interface HttpErrorMap {}
|
|
23
|
+
export type HttpSuccessData<TKey extends HttpKey = HttpKey> = Exclude<HttpResponseData<TKey>, HttpError>
|
|
24
|
+
|
|
25
|
+
export interface UseHttpCache {
|
|
26
|
+
deleteKeysWithPart: (part: string) => Promise<void>
|
|
27
|
+
get: <T>(key: string) => Promise<null | UseHttpCacheEntry<T>>
|
|
28
|
+
set: <T>(key: string, value: T, ttl: number) => Promise<void>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface UseHttpCacheEntry<T> {
|
|
32
|
+
hash: string
|
|
33
|
+
value: T
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface UseHttpFn {
|
|
37
|
+
<T extends HttpKey, P extends HttpParam>(options: UseHttpOptions<T, P>): Promise<UseHttpResult<T, P>>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface UseHttpOptions<T extends HttpKey, P extends HttpParam> {
|
|
41
|
+
effect?: UseHttpEffect<T, P>
|
|
42
|
+
initParams?: P
|
|
43
|
+
isError?: (payload: HttpResponseData<T>) => boolean
|
|
44
|
+
lazy?: true
|
|
45
|
+
mapParams?: (params?: P) => P
|
|
46
|
+
server?: boolean
|
|
47
|
+
url: T
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UseHttpResult<T extends HttpKey, P extends HttpParam> {
|
|
51
|
+
data: HttpSuccessData<T> | null
|
|
52
|
+
error: null | UseHttpError<T>
|
|
53
|
+
fetch: UseHttpFetch<P>
|
|
54
|
+
hasFirstData: boolean
|
|
55
|
+
hasFreshData: boolean
|
|
56
|
+
pending: boolean
|
|
57
|
+
pendingCache: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type BroadcastMessage = {
|
|
61
|
+
data: unknown
|
|
62
|
+
fullUrl: string
|
|
63
|
+
params: HttpParam
|
|
64
|
+
type: 'STATE_UPDATE'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type UseHttpEffect<T extends HttpKey, P extends HttpParam> = (
|
|
68
|
+
data: HttpResponseData<T>,
|
|
69
|
+
config: UseHttpEffectConfig<P>,
|
|
70
|
+
) => void
|
|
71
|
+
type UseHttpEffectConfig<P extends HttpParam> = {
|
|
72
|
+
cached: boolean
|
|
73
|
+
params: P
|
|
74
|
+
}
|
|
75
|
+
type UseHttpError<T extends HttpKey> = Extract<HttpResponseData<T>, HttpError>
|
|
76
|
+
|
|
77
|
+
type UseHttpFetch<P extends HttpParam> = (params?: P, opt?: { signal: AbortSignal }) => Promise<void>
|
|
78
|
+
|
|
79
|
+
export function createUseHttp(dependencies: CreateUseHttpDependencies): UseHttpFn {
|
|
80
|
+
let channel: BroadcastChannel | null
|
|
81
|
+
|
|
82
|
+
const useHttp: UseHttpFn = async <T extends HttpKey, P extends HttpParam>(
|
|
83
|
+
options: UseHttpOptions<T, P>,
|
|
84
|
+
): Promise<UseHttpResult<T, P>> => {
|
|
85
|
+
const mapParams = (params?: P): P => {
|
|
86
|
+
if (options.mapParams) {
|
|
87
|
+
return options.mapParams(params)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return params ?? ({} as P)
|
|
91
|
+
}
|
|
92
|
+
const effect = options.effect
|
|
93
|
+
const isError = createIsErrorGuard(options.isError, dependencies.isError)
|
|
94
|
+
|
|
95
|
+
const buildUrl = createURL
|
|
96
|
+
const initFullUrl = buildUrl(options.url, mapParams(options.initParams))
|
|
97
|
+
const httpClient = dependencies.getHttpClient()
|
|
98
|
+
const cache = import.meta.client ? (dependencies.getCache?.() ?? null) : null
|
|
99
|
+
let hasDataFromServer = false
|
|
100
|
+
|
|
101
|
+
const result = shallowReactive({
|
|
102
|
+
data: null as null | unknown,
|
|
103
|
+
error: null as null | unknown,
|
|
104
|
+
fetch: async (_params?: P, _opt?: { signal: AbortSignal }): Promise<void> => await undefined,
|
|
105
|
+
hasFirstData: false,
|
|
106
|
+
hasFreshData: false,
|
|
107
|
+
pending: true,
|
|
108
|
+
pendingCache: true,
|
|
109
|
+
})
|
|
110
|
+
const setError = (value: null | unknown): void => {
|
|
111
|
+
result.error = value
|
|
112
|
+
}
|
|
113
|
+
const setData = (value: null | unknown): void => {
|
|
114
|
+
result.data = value
|
|
115
|
+
}
|
|
116
|
+
const syncResult = (payload: HttpResponseData<T> | null | undefined): void => {
|
|
117
|
+
if (payload === null || payload === undefined) {
|
|
118
|
+
setData(null)
|
|
119
|
+
setError(null)
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (isError(payload)) {
|
|
124
|
+
setData(null)
|
|
125
|
+
setError(payload)
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
setData(payload)
|
|
130
|
+
setError(null)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const controller = new AbortController()
|
|
134
|
+
const serverData = useState<null | unknown>(`http-${initFullUrl}`, () => null)
|
|
135
|
+
|
|
136
|
+
if (options.server && import.meta.server) {
|
|
137
|
+
const paramsReactive = shallowRef(mapParams(options.initParams))
|
|
138
|
+
const ssr = await useLazyAsyncData(initFullUrl, async () => {
|
|
139
|
+
return await httpClient.get<T>(options.url, {
|
|
140
|
+
params: paramsReactive.value,
|
|
141
|
+
} as GetConfig<T>)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
result.fetch = async (params?: P) => {
|
|
145
|
+
paramsReactive.value = mapParams(params)
|
|
146
|
+
await ssr.refresh()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
serverData.value = ssr.data.value?.data ?? null
|
|
150
|
+
const serverPayload = serverData.value as HttpResponseData<T>
|
|
151
|
+
syncResult(serverPayload)
|
|
152
|
+
|
|
153
|
+
result.pending = false
|
|
154
|
+
result.pendingCache = false
|
|
155
|
+
result.hasFirstData = true
|
|
156
|
+
result.hasFreshData = true
|
|
157
|
+
|
|
158
|
+
if (serverData.value) {
|
|
159
|
+
effect?.(serverData.value as HttpResponseData<T>, {
|
|
160
|
+
cached: false,
|
|
161
|
+
params: mapParams(options.initParams),
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (result.data) {
|
|
166
|
+
hasDataFromServer = true
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (import.meta.client) {
|
|
171
|
+
channel = getChannel(dependencies.channelName ?? DEFAULT_CHANNEL_NAME, channel)
|
|
172
|
+
const fullUrlHistory: Record<string, true> = {}
|
|
173
|
+
|
|
174
|
+
function onMessage(event: MessageEvent<Partial<BroadcastMessage>>): void {
|
|
175
|
+
if (event.data.fullUrl && fullUrlHistory[event.data.fullUrl]) {
|
|
176
|
+
const eventPayload = event.data.data as HttpResponseData<T> | undefined
|
|
177
|
+
|
|
178
|
+
if (eventPayload) {
|
|
179
|
+
effect?.(eventPayload, {
|
|
180
|
+
cached: false,
|
|
181
|
+
params: event.data.params as P,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
syncResult(eventPayload)
|
|
186
|
+
|
|
187
|
+
result.hasFirstData = true
|
|
188
|
+
result.hasFreshData = true
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
channel?.addEventListener('message', onMessage)
|
|
193
|
+
|
|
194
|
+
if (serverData.value) {
|
|
195
|
+
const clientServerPayload = serverData.value as HttpResponseData<T>
|
|
196
|
+
syncResult(clientServerPayload)
|
|
197
|
+
result.pending = false
|
|
198
|
+
result.pendingCache = false
|
|
199
|
+
result.hasFirstData = true
|
|
200
|
+
result.hasFreshData = true
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const raceCondition: Record<string, number> = {}
|
|
204
|
+
const ttl = dependencies.ttl ?? DEFAULT_TTL
|
|
205
|
+
|
|
206
|
+
const runFetch = async (params?: P, fetchOpt?: { signal?: AbortSignal }): Promise<void> => {
|
|
207
|
+
const mappedParams = mapParams(params)
|
|
208
|
+
const fullUrl = buildUrl(options.url, mappedParams)
|
|
209
|
+
const fetchId = Date.now() + getRandom(0, 300)
|
|
210
|
+
|
|
211
|
+
if (raceCondition[fullUrl]) {
|
|
212
|
+
console.info('Race Condition affect', fullUrl)
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
raceCondition[fullUrl] = fetchId
|
|
217
|
+
|
|
218
|
+
try {
|
|
219
|
+
result.pending = true
|
|
220
|
+
result.pendingCache = true
|
|
221
|
+
|
|
222
|
+
const cachedFetch = cache && !dependencies.isDev?.() ? await cache.get<unknown>(fullUrl) : null
|
|
223
|
+
|
|
224
|
+
if (cachedFetch) {
|
|
225
|
+
const cachedPayload = cachedFetch.value as HttpResponseData<T>
|
|
226
|
+
|
|
227
|
+
effect?.(cachedPayload, {
|
|
228
|
+
cached: true,
|
|
229
|
+
params: mappedParams,
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
syncResult(cachedPayload)
|
|
233
|
+
result.hasFirstData = true
|
|
234
|
+
|
|
235
|
+
result.pendingCache = false
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (controller.signal.aborted || fetchOpt?.signal?.aborted) {
|
|
239
|
+
throw new DOMException('Aborted', 'AbortError')
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const signalHttp = fetchOpt?.signal ? [controller.signal, fetchOpt.signal] : controller.signal
|
|
243
|
+
const response = await httpClient.get<T>(options.url, {
|
|
244
|
+
params: mappedParams,
|
|
245
|
+
signal: signalHttp,
|
|
246
|
+
} as GetConfig<T>)
|
|
247
|
+
const responsePayload = response.data as HttpResponseData<T>
|
|
248
|
+
|
|
249
|
+
effect?.(responsePayload, {
|
|
250
|
+
cached: false,
|
|
251
|
+
params: mappedParams,
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
if (isError(responsePayload)) {
|
|
255
|
+
syncResult(responsePayload)
|
|
256
|
+
} else {
|
|
257
|
+
syncResult(responsePayload)
|
|
258
|
+
const successData = responsePayload
|
|
259
|
+
|
|
260
|
+
fullUrlHistory[fullUrl] = true
|
|
261
|
+
channel?.postMessage({
|
|
262
|
+
data: normalizeBroadcastValue(successData),
|
|
263
|
+
fullUrl,
|
|
264
|
+
params: normalizeBroadcastValue(mappedParams),
|
|
265
|
+
type: 'STATE_UPDATE',
|
|
266
|
+
} satisfies BroadcastMessage)
|
|
267
|
+
|
|
268
|
+
if (cache) {
|
|
269
|
+
if (cachedFetch) {
|
|
270
|
+
const newHash = await hashData(responsePayload)
|
|
271
|
+
if (newHash !== cachedFetch.hash) {
|
|
272
|
+
await cache.deleteKeysWithPart(options.url)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (response.status === 200) {
|
|
277
|
+
await cache.set(fullUrl, successData, ttl)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
result.hasFirstData = true
|
|
283
|
+
result.hasFreshData = true
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.error(error)
|
|
286
|
+
} finally {
|
|
287
|
+
if (raceCondition[fullUrl] === fetchId) {
|
|
288
|
+
delete raceCondition[fullUrl]
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
result.pending = false
|
|
292
|
+
result.pendingCache = false
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
result.fetch = runFetch
|
|
297
|
+
|
|
298
|
+
if (!hasDataFromServer && options.lazy !== true) {
|
|
299
|
+
runFetch(options.initParams)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
onScopeDispose(() => {
|
|
303
|
+
channel?.removeEventListener('message', onMessage)
|
|
304
|
+
controller.abort(`Http Abort -> onScopeDispose ${options.url}`)
|
|
305
|
+
|
|
306
|
+
if (serverData.value) {
|
|
307
|
+
serverData.value = null
|
|
308
|
+
}
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return result as UseHttpResult<T, P>
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return useHttp
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function createIsErrorGuard<TData>(
|
|
319
|
+
localGuard?: (payload: TData) => boolean,
|
|
320
|
+
globalGuard?: HttpErrorGuard<unknown>,
|
|
321
|
+
): (payload: TData) => boolean {
|
|
322
|
+
if (localGuard) {
|
|
323
|
+
return localGuard
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (globalGuard) {
|
|
327
|
+
return globalGuard as (payload: TData) => boolean
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return (): boolean => false
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function getChannel(name: string, currentChannel?: BroadcastChannel | null): BroadcastChannel | null {
|
|
334
|
+
if (currentChannel) {
|
|
335
|
+
return currentChannel
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (import.meta.server || typeof BroadcastChannel !== 'function') {
|
|
339
|
+
return null
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return new BroadcastChannel(name)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function getRandom(min: number, max: number): number {
|
|
346
|
+
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function normalizeBroadcastValue<T>(payload: T): T {
|
|
350
|
+
try {
|
|
351
|
+
return structuredClone(payload)
|
|
352
|
+
} catch {
|
|
353
|
+
return JSON.parse(JSON.stringify(payload)) as T
|
|
354
|
+
}
|
|
355
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { HttpParam } from './http'
|
|
2
|
+
|
|
3
|
+
export function createURL(url: string, params?: HttpParam): string {
|
|
4
|
+
const urlParams = new URLSearchParams()
|
|
5
|
+
|
|
6
|
+
Object.entries(params ?? {}).forEach(([key, value]) => {
|
|
7
|
+
if (value === undefined) {
|
|
8
|
+
return
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (Array.isArray(value)) {
|
|
12
|
+
value.forEach((item) => {
|
|
13
|
+
urlParams.append(`${key}[]`, item)
|
|
14
|
+
})
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
urlParams.append(key, String(value))
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const query = urlParams.size > 0 ? `?${urlParams}` : ''
|
|
22
|
+
return urlParams.size > 0 ? `${url}${query}` : url
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function fastDevHash(data: unknown): string {
|
|
26
|
+
const str = JSON.stringify(data)
|
|
27
|
+
let hash = 0
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < str.length; i++) {
|
|
30
|
+
const chr = str.charCodeAt(i)
|
|
31
|
+
hash = (hash << 5) - hash + chr
|
|
32
|
+
hash |= 0
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return Math.abs(hash).toString(16)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function hashData(data: unknown): Promise<string> {
|
|
39
|
+
const subtle = globalThis.crypto?.subtle
|
|
40
|
+
|
|
41
|
+
if (subtle) {
|
|
42
|
+
const encoded = new TextEncoder().encode(JSON.stringify(data))
|
|
43
|
+
const buffer = await subtle.digest('SHA-1', encoded)
|
|
44
|
+
const array = Array.from(new Uint8Array(buffer))
|
|
45
|
+
|
|
46
|
+
return array.map((value) => value.toString(16).padStart(2, '0')).join('')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return fastDevHash(data)
|
|
50
|
+
}
|
package/dist/module.d.mts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
import { HttpRetryConfig } from '../dist/runtime/utils/shared.js';
|
|
3
|
-
export { CreateHttpClientOptions, GetConfig, HttpBaseURL, HttpBaseURLResolver, HttpClient, HttpConfig, HttpErrorPayload, HttpParam, HttpPayload, HttpRequestContext, HttpRequestMiddleware, HttpResponse, HttpResponseMiddleware, HttpRetryConfig, HttpRuntimeConfig, PostConfig } from '../dist/runtime/utils/shared.js';
|
|
4
|
-
export { createHttpClient } from '../dist/runtime/http/client.js';
|
|
5
|
-
export { addHttpRequestMiddleware, addHttpResponseMiddleware, removeHttpRequestMiddleware, removeHttpResponseMiddleware } from '../dist/runtime/utils/middleware.js';
|
|
6
|
-
export { HttpRouteBody, HttpRouteData, HttpRouteDefinition, HttpRouteError, HttpRouteMap, HttpRouteParams, ResolveHttpRoute, StrictTypedHttpClient, TypedGetConfig, TypedHttpClient, TypedHttpResponse, TypedPostConfig, createStrictHttpClient, createTypedHttpClient, defineHttpRoutes } from '../dist/runtime/utils/typed.js';
|
|
7
|
-
|
|
8
|
-
interface ModuleOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Base URL used by the injected Nuxt HTTP client.
|
|
11
|
-
*
|
|
12
|
-
* @default ''
|
|
13
|
-
*/
|
|
14
|
-
baseURL?: string;
|
|
15
|
-
/**
|
|
16
|
-
* Enables client-side IndexedDB caching in `useHttp()`.
|
|
17
|
-
*
|
|
18
|
-
* @default true
|
|
19
|
-
*/
|
|
20
|
-
cache?: boolean;
|
|
21
|
-
/**
|
|
22
|
-
* IndexedDB database name used for cached responses.
|
|
23
|
-
*
|
|
24
|
-
* @default 'smart-cache-v2'
|
|
25
|
-
*/
|
|
26
|
-
cacheDbName?: 'smart-cache-v2';
|
|
27
|
-
/**
|
|
28
|
-
* IndexedDB store name used for cached responses.
|
|
29
|
-
*
|
|
30
|
-
* @default 'data'
|
|
31
|
-
*/
|
|
32
|
-
cacheStoreName?: string;
|
|
33
|
-
/**
|
|
34
|
-
* Cache TTL in milliseconds.
|
|
35
|
-
*
|
|
36
|
-
* @default 604800000
|
|
37
|
-
*/
|
|
38
|
-
cacheTtlMs?: number;
|
|
39
|
-
/**
|
|
40
|
-
* Adds `Client-Env: development` in dev mode.
|
|
41
|
-
*
|
|
42
|
-
* @default true
|
|
43
|
-
*/
|
|
44
|
-
clientEnvHeader?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Default headers merged into every request.
|
|
47
|
-
*
|
|
48
|
-
* @default {}
|
|
49
|
-
*/
|
|
50
|
-
defaultHeaders?: Record<string, string>;
|
|
51
|
-
/**
|
|
52
|
-
* Disables IndexedDB cache when `import.meta.dev` is enabled.
|
|
53
|
-
*
|
|
54
|
-
* @default true
|
|
55
|
-
*/
|
|
56
|
-
disableCacheInDev?: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Request timeout in milliseconds.
|
|
59
|
-
*
|
|
60
|
-
* @default 80000
|
|
61
|
-
*/
|
|
62
|
-
requestTimeoutMs?: number;
|
|
63
|
-
/**
|
|
64
|
-
* Retry policy for GET requests on retryable responses and network failures.
|
|
65
|
-
*
|
|
66
|
-
* @default { delay: 300, retries: 3 }
|
|
67
|
-
*/
|
|
68
|
-
retry?: HttpRetryConfig;
|
|
69
|
-
}
|
|
70
|
-
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
71
|
-
|
|
72
|
-
export { _default as default };
|
|
73
|
-
export type { ModuleOptions };
|
package/dist/module.json
DELETED
package/dist/module.mjs
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addPlugin, addImportsDir } from '@nuxt/kit';
|
|
2
|
-
export { createHttpClient } from '../dist/runtime/http/client.js';
|
|
3
|
-
export { addHttpRequestMiddleware, addHttpResponseMiddleware, removeHttpRequestMiddleware, removeHttpResponseMiddleware } from '../dist/runtime/utils/middleware.js';
|
|
4
|
-
export { createStrictHttpClient, createTypedHttpClient, defineHttpRoutes } from '../dist/runtime/utils/typed.js';
|
|
5
|
-
|
|
6
|
-
const DAY = 1e3 * 60 * 60 * 24;
|
|
7
|
-
const defaultRuntimeConfig = {
|
|
8
|
-
baseURL: "",
|
|
9
|
-
cache: true,
|
|
10
|
-
cacheDbName: "smart-cache-v2",
|
|
11
|
-
cacheStoreName: "data",
|
|
12
|
-
cacheTtlMs: DAY * 7,
|
|
13
|
-
clientEnvHeader: true,
|
|
14
|
-
defaultHeaders: {},
|
|
15
|
-
disableCacheInDev: true,
|
|
16
|
-
requestTimeoutMs: 8e4,
|
|
17
|
-
retry: {
|
|
18
|
-
delay: 300,
|
|
19
|
-
retries: 3
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
const module$1 = defineNuxtModule({
|
|
23
|
-
defaults: defaultRuntimeConfig,
|
|
24
|
-
meta: {
|
|
25
|
-
compatibility: {
|
|
26
|
-
nuxt: ">=4.0.0"
|
|
27
|
-
},
|
|
28
|
-
configKey: "brickflowHttp",
|
|
29
|
-
name: "@brickflow/http"
|
|
30
|
-
},
|
|
31
|
-
setup(options, nuxt) {
|
|
32
|
-
const resolver = createResolver(import.meta.url);
|
|
33
|
-
const currentConfig = nuxt.options.runtimeConfig.public.brickflowHttp ?? {};
|
|
34
|
-
nuxt.options.runtimeConfig.public.brickflowHttp = {
|
|
35
|
-
...defaultRuntimeConfig,
|
|
36
|
-
...currentConfig,
|
|
37
|
-
...options,
|
|
38
|
-
defaultHeaders: {
|
|
39
|
-
...defaultRuntimeConfig.defaultHeaders,
|
|
40
|
-
...currentConfig.defaultHeaders ?? {},
|
|
41
|
-
...options.defaultHeaders ?? {}
|
|
42
|
-
},
|
|
43
|
-
retry: {
|
|
44
|
-
...defaultRuntimeConfig.retry,
|
|
45
|
-
...currentConfig.retry ?? {},
|
|
46
|
-
...options.retry ?? {}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
50
|
-
addImportsDir(resolver.resolve("./runtime/composables"));
|
|
51
|
-
nuxt.hook("prepare:types", ({ references }) => {
|
|
52
|
-
references.push({
|
|
53
|
-
path: resolver.resolve("./runtime/types.d.ts")
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export { module$1 as default };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { type ShallowReactive } from 'vue';
|
|
2
|
-
import type { HttpRouteData, HttpRouteError, HttpRouteMap, HttpRouteParams, ResolveHttpRoute } from '../utils/typed.js';
|
|
3
|
-
import { type HttpErrorPayload, type HttpParam, type HttpPayload } from '../utils/shared.js';
|
|
4
|
-
type UseHttpState<TData, TError extends HttpErrorPayload, TParams extends HttpParam> = ShallowReactive<{
|
|
5
|
-
data: null | TData;
|
|
6
|
-
error: null | TError;
|
|
7
|
-
fetch: (params?: TParams, opt?: {
|
|
8
|
-
signal: AbortSignal;
|
|
9
|
-
}) => Promise<void>;
|
|
10
|
-
hasFirstData: boolean;
|
|
11
|
-
hasFreshData: boolean;
|
|
12
|
-
pending: boolean;
|
|
13
|
-
pendingCache: boolean;
|
|
14
|
-
}>;
|
|
15
|
-
export declare function useHttp<TUrl extends Extract<keyof HttpRouteMap, string>>(options: {
|
|
16
|
-
effect?: (payload: HttpPayload<HttpRouteData<ResolveHttpRoute<HttpRouteMap, TUrl>>, HttpRouteError<ResolveHttpRoute<HttpRouteMap, TUrl>>>, config: {
|
|
17
|
-
cached: boolean;
|
|
18
|
-
params: HttpRouteParams<ResolveHttpRoute<HttpRouteMap, TUrl>>;
|
|
19
|
-
}) => undefined | void;
|
|
20
|
-
initParams?: HttpRouteParams<ResolveHttpRoute<HttpRouteMap, TUrl>>;
|
|
21
|
-
lazy?: true;
|
|
22
|
-
mapParams?: <TMapped extends HttpRouteParams<ResolveHttpRoute<HttpRouteMap, TUrl>>>(params?: TMapped) => TMapped;
|
|
23
|
-
server?: boolean;
|
|
24
|
-
url: TUrl;
|
|
25
|
-
}): Promise<UseHttpState<HttpRouteData<ResolveHttpRoute<HttpRouteMap, TUrl>>, HttpRouteError<ResolveHttpRoute<HttpRouteMap, TUrl>>, HttpRouteParams<ResolveHttpRoute<HttpRouteMap, TUrl>>>>;
|
|
26
|
-
export {};
|
|
27
|
-
//# sourceMappingURL=useHttp.d.ts.map
|