astro.mjs
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export const makeLeadingCase = (s) => s.substring(0, 1).toUpperCase() + s.substring(1)
export const fixUnsubscribe = async (store, action) => {
const promise = store.dispatch(action)
try {
return await promise
} finally {
if (promise.unsubscribe) promise.unsubscribe()
}
}
export const DefaultEndpointOptions = Symbol.for('DefaultEndpointOptions')
export const createApiWrappers = (apiSlice, options = {}) => {
const { util: { upsertQueryData } } = apiSlice
const { prefix, endpoints: endpointOptions = {} } = options
const { [ DefaultEndpointOptions ]: defaultEndpointOptions = {} } = endpointOptions
return Object.entries(apiSlice.endpoints).reduce((result, [ endpointName, endpoint ]) => {
const { initiate } = endpoint
const methodName = prefix ? `${prefix}${makeLeadingCase(endpointName)}` : endpointName
const { [ endpointName ]: { forceRefetch = false } = defaultEndpointOptions } = endpointOptions
result.endpoints[ methodName ] = (store, arg) => {
return fixUnsubscribe(store, initiate(arg, { forceRefetch, subscribe: true }))
}
result.upserts[ methodName ] = (store, arg, data) => {
return fixUnsubscribe(store, upsertQueryData(endpointName, arg, data))
}
return result
}, { endpoints: {}, upserts: {} })
}
export const createAstroApiWrappers = (apiWrappers) => Object.entries(apiWrappers).reduce((result, [ apiType, methods ]) => {
result[ apiType ] = Object.entries(methods).reduce((methods, [ methodName, method ]) => {
methods[ methodName ] = (Astro, ...args) => method(Astro.locals.store, ...args)
return methods
}, {})
return result
}, {})
export { default as AstroReduxProvider } from './Provider.astro'
export { default as RefreshToken } from './RefreshToken.astro'
export { default as RefreshTokenReact } from './RefreshToken.jsx'