@swoff/cli 0.3.9 → 0.3.10
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/dist/__tests__/assemble-sw.test.js +8 -17
- package/dist/__tests__/assemble-sw.test.js.map +1 -1
- package/dist/__tests__/cli-integration.test.js +2 -2
- package/dist/__tests__/cli-integration.test.js.map +1 -1
- package/dist/__tests__/config-types.test.js +18 -8
- package/dist/__tests__/config-types.test.js.map +1 -1
- package/dist/__tests__/file-generators.test.js +3 -3
- package/dist/__tests__/file-generators.test.js.map +1 -1
- package/dist/__tests__/loader.test.js +5 -1
- package/dist/__tests__/loader.test.js.map +1 -1
- package/dist/__tests__/validator.test.js +6 -13
- package/dist/__tests__/validator.test.js.map +1 -1
- package/dist/index.js +39 -35
- package/dist/index.js.map +1 -1
- package/dist/lib/cli/help.js +13 -8
- package/dist/lib/cli/help.js.map +1 -1
- package/dist/lib/commands/add.js +38 -8
- package/dist/lib/commands/add.js.map +1 -1
- package/dist/lib/commands/assets.js +37 -0
- package/dist/lib/commands/assets.js.map +1 -0
- package/dist/lib/commands/clean.js +33 -17
- package/dist/lib/commands/clean.js.map +1 -1
- package/dist/lib/commands/generate-assets.js +44 -0
- package/dist/lib/commands/generate-assets.js.map +1 -0
- package/dist/lib/commands/generate-guide.js +22 -29
- package/dist/lib/commands/generate-guide.js.map +1 -1
- package/dist/lib/commands/generate.js +16 -9
- package/dist/lib/commands/generate.js.map +1 -1
- package/dist/lib/commands/info.js +36 -45
- package/dist/lib/commands/info.js.map +1 -1
- package/dist/lib/commands/init.js +25 -3
- package/dist/lib/commands/init.js.map +1 -1
- package/dist/lib/commands/validate.js +1 -1
- package/dist/lib/commands/validate.js.map +1 -1
- package/dist/lib/config/loader.js +25 -20
- package/dist/lib/config/loader.js.map +1 -1
- package/dist/lib/config/validator.js +20 -11
- package/dist/lib/config/validator.js.map +1 -1
- package/dist/lib/generators/asset-generator/dependency-resolver.js +76 -0
- package/dist/lib/generators/asset-generator/dependency-resolver.js.map +1 -0
- package/dist/lib/generators/asset-generator/generate.js +111 -0
- package/dist/lib/generators/asset-generator/generate.js.map +1 -0
- package/dist/lib/generators/asset-generator/guide.js +43 -0
- package/dist/lib/generators/asset-generator/guide.js.map +1 -0
- package/dist/lib/generators/asset-generator/html-patcher.js +65 -0
- package/dist/lib/generators/asset-generator/html-patcher.js.map +1 -0
- package/dist/lib/generators/asset-generator/ico-encoder.js +29 -0
- package/dist/lib/generators/asset-generator/ico-encoder.js.map +1 -0
- package/dist/lib/generators/asset-generator/manifest-patcher.js +23 -0
- package/dist/lib/generators/asset-generator/manifest-patcher.js.map +1 -0
- package/dist/lib/generators/asset-generator/maskable.js +15 -0
- package/dist/lib/generators/asset-generator/maskable.js.map +1 -0
- package/dist/lib/generators/asset-generator/rasterize.js +32 -0
- package/dist/lib/generators/asset-generator/rasterize.js.map +1 -0
- package/dist/lib/generators/asset-generator/sizes.js +25 -0
- package/dist/lib/generators/asset-generator/sizes.js.map +1 -0
- package/dist/lib/generators/file-generators/api-config.js +6 -0
- package/dist/lib/generators/file-generators/api-config.js.map +1 -0
- package/dist/lib/generators/file-generators/auth-state.js +2 -34
- package/dist/lib/generators/file-generators/auth-state.js.map +1 -1
- package/dist/lib/generators/file-generators/auth-store.js +2 -250
- package/dist/lib/generators/file-generators/auth-store.js.map +1 -1
- package/dist/lib/generators/file-generators/auth-user.js +3 -81
- package/dist/lib/generators/file-generators/auth-user.js.map +1 -1
- package/dist/lib/generators/file-generators/background-sync.js +2 -57
- package/dist/lib/generators/file-generators/background-sync.js.map +1 -1
- package/dist/lib/generators/file-generators/cache.js +2 -30
- package/dist/lib/generators/file-generators/cache.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector.js +2 -130
- package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/fetch-state.js +6 -0
- package/dist/lib/generators/file-generators/fetch-state.js.map +1 -0
- package/dist/lib/generators/file-generators/fetch-wrapper.js +3 -295
- package/dist/lib/generators/file-generators/fetch-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/generate-framework-adapters.js +56 -0
- package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -0
- package/dist/lib/generators/file-generators/generate-hooks.js +4 -3
- package/dist/lib/generators/file-generators/generate-hooks.js.map +1 -1
- package/dist/lib/generators/file-generators/gql-wrapper.js +3 -132
- package/dist/lib/generators/file-generators/gql-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/guide-generator.js +29 -1
- package/dist/lib/generators/file-generators/guide-generator.js.map +1 -1
- package/dist/lib/generators/file-generators/invalidation-tags.js +3 -258
- package/dist/lib/generators/file-generators/invalidation-tags.js.map +1 -1
- package/dist/lib/generators/file-generators/manifest.js +13 -3
- package/dist/lib/generators/file-generators/manifest.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-queue.js +3 -305
- package/dist/lib/generators/file-generators/mutation-queue.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-state.js +2 -117
- package/dist/lib/generators/file-generators/mutation-state.js.map +1 -1
- package/dist/lib/generators/file-generators/push.js +6 -138
- package/dist/lib/generators/file-generators/push.js.map +1 -1
- package/dist/lib/generators/file-generators/pwa-install.js +11 -84
- package/dist/lib/generators/file-generators/pwa-install.js.map +1 -1
- package/dist/lib/generators/file-generators/reset.js +6 -0
- package/dist/lib/generators/file-generators/reset.js.map +1 -0
- package/dist/lib/generators/file-generators/server-push.js +3 -134
- package/dist/lib/generators/file-generators/server-push.js.map +1 -1
- package/dist/lib/generators/file-generators/sw-generator-build.js +4 -4
- package/dist/lib/generators/file-generators/sw-injector.js +9 -230
- package/dist/lib/generators/file-generators/sw-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/type-definitions.js +2 -111
- package/dist/lib/generators/file-generators/type-definitions.js.map +1 -1
- package/dist/lib/generators/sw-generator.js +5 -5
- package/dist/lib/generators/sw-generator.js.map +1 -1
- package/dist/lib/generators/sw-sections/activate-handler.js +25 -3
- package/dist/lib/generators/sw-sections/activate-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/assemble-sw.js +13 -12
- package/dist/lib/generators/sw-sections/assemble-sw.js.map +1 -1
- package/dist/lib/generators/sw-sections/background-sync-handler.js +14 -7
- package/dist/lib/generators/sw-sections/background-sync-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/config-header.js +1 -1
- package/dist/lib/generators/sw-sections/config-header.js.map +1 -1
- package/dist/lib/generators/sw-sections/fetch-handler.js +165 -104
- package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/glob-matcher-code.js.map +1 -0
- package/dist/lib/generators/sw-sections/server-push-handler.js +12 -0
- package/dist/lib/generators/sw-sections/server-push-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/tag-management.js +6 -4
- package/dist/lib/generators/sw-sections/tag-management.js.map +1 -1
- package/dist/lib/generators/swoff-files-generator.js +66 -35
- package/dist/lib/generators/swoff-files-generator.js.map +1 -1
- package/dist/lib/shared/config-types.js +89 -28
- package/dist/lib/shared/config-types.js.map +1 -1
- package/dist/runtime/api-config.js +18 -0
- package/dist/runtime/api-config.js.map +1 -0
- package/dist/runtime/auth-state.js +35 -0
- package/dist/runtime/auth-state.js.map +1 -0
- package/dist/runtime/auth-store.js +273 -0
- package/dist/runtime/auth-store.js.map +1 -0
- package/dist/runtime/auth-user.js +168 -0
- package/dist/runtime/auth-user.js.map +1 -0
- package/dist/runtime/background-sync.js +59 -0
- package/dist/runtime/background-sync.js.map +1 -0
- package/dist/runtime/cache.js +30 -0
- package/dist/runtime/cache.js.map +1 -0
- package/dist/runtime/client-injector.js +136 -0
- package/dist/runtime/client-injector.js.map +1 -0
- package/dist/runtime/fetch-state.js +42 -0
- package/dist/runtime/fetch-state.js.map +1 -0
- package/dist/runtime/fetch-wrapper.js +355 -0
- package/dist/runtime/fetch-wrapper.js.map +1 -0
- package/dist/runtime/gql-wrapper.js +135 -0
- package/dist/runtime/gql-wrapper.js.map +1 -0
- package/dist/runtime/invalidation-tags.js +260 -0
- package/dist/runtime/invalidation-tags.js.map +1 -0
- package/dist/runtime/mutation-queue.js +304 -0
- package/dist/runtime/mutation-queue.js.map +1 -0
- package/dist/runtime/mutation-state.js +117 -0
- package/dist/runtime/mutation-state.js.map +1 -0
- package/dist/runtime/push.js +138 -0
- package/dist/runtime/push.js.map +1 -0
- package/dist/runtime/pwa-index.js +11 -0
- package/dist/runtime/pwa-index.js.map +1 -0
- package/dist/runtime/pwa-injector.js +44 -0
- package/dist/runtime/pwa-injector.js.map +1 -0
- package/dist/runtime/pwa-install.js +83 -0
- package/dist/runtime/pwa-install.js.map +1 -0
- package/dist/runtime/pwa-prompt.js +54 -0
- package/dist/runtime/pwa-prompt.js.map +1 -0
- package/dist/runtime/reset.js +148 -0
- package/dist/runtime/reset.js.map +1 -0
- package/dist/runtime/server-push.js +148 -0
- package/dist/runtime/server-push.js.map +1 -0
- package/dist/runtime/sw-injector.js +236 -0
- package/dist/runtime/sw-injector.js.map +1 -0
- package/dist/runtime/type-definitions.js +138 -0
- package/dist/runtime/type-definitions.js.map +1 -0
- package/dist/runtime/utils.js +27 -0
- package/dist/runtime/utils.js.map +1 -0
- package/package.json +3 -2
- package/templates/react/useAuth.jsx +106 -0
- package/templates/react/useAuth.tsx +118 -0
- package/templates/{hooks → react}/useBackgroundSync.jsx +6 -4
- package/templates/{hooks → react}/useBackgroundSync.tsx +6 -4
- package/templates/{hooks → react}/useCacheInvalidation.jsx +2 -2
- package/templates/react/useCacheInvalidation.tsx +52 -0
- package/templates/react/useCachedFetch.jsx +140 -0
- package/templates/react/useCachedFetch.tsx +160 -0
- package/templates/react/useIsFetching.jsx +32 -0
- package/templates/react/useIsFetching.tsx +32 -0
- package/templates/react/useMutation.jsx +141 -0
- package/templates/react/useMutation.tsx +180 -0
- package/templates/react/useMutationQueue.jsx +69 -0
- package/templates/react/useMutationQueue.tsx +83 -0
- package/templates/react/useNetworkStatus.jsx +61 -0
- package/templates/react/useNetworkStatus.tsx +76 -0
- package/templates/react/usePrefetch.jsx +32 -0
- package/templates/react/usePrefetch.tsx +42 -0
- package/templates/{hooks → react}/usePushSubscription.jsx +4 -4
- package/templates/{hooks → react}/usePushSubscription.tsx +4 -4
- package/templates/{hooks → react}/useSWUpdate.jsx +3 -3
- package/templates/{hooks → react}/useSWUpdate.tsx +4 -4
- package/templates/react/useSwoffReset.jsx +32 -0
- package/templates/react/useSwoffReset.tsx +39 -0
- package/docs/API.md +0 -565
- package/docs/ARCHITECTURE.md +0 -374
- package/docs/CLI.md +0 -148
- package/docs/COMPARISON.md +0 -192
- package/docs/CONFIG.md +0 -277
- package/docs/ECOSYSTEM.md +0 -33
- package/templates/hooks/useAuth.jsx +0 -30
- package/templates/hooks/useAuth.tsx +0 -30
- package/templates/hooks/useCacheInvalidation.tsx +0 -19
- package/templates/hooks/useCachedFetch.jsx +0 -69
- package/templates/hooks/useCachedFetch.tsx +0 -81
- package/templates/hooks/useMutation.jsx +0 -76
- package/templates/hooks/useMutation.tsx +0 -109
- package/templates/hooks/useMutationQueue.jsx +0 -43
- package/templates/hooks/useMutationQueue.tsx +0 -50
- package/templates/hooks/useNetworkStatus.jsx +0 -19
- package/templates/hooks/useNetworkStatus.tsx +0 -19
- package/templates/hooks/usePrefetch.jsx +0 -10
- package/templates/hooks/usePrefetch.tsx +0 -25
- /package/templates/{hooks → react}/useMutationState.jsx +0 -0
- /package/templates/{hooks → react}/useMutationState.tsx +0 -0
|
@@ -3,303 +3,11 @@
|
|
|
3
3
|
* staleTime, prefetching, and cancellation.
|
|
4
4
|
*/
|
|
5
5
|
import { writeFile } from "./context.js";
|
|
6
|
+
import { generateFetchWrapperCode } from "../../../runtime/fetch-wrapper.js";
|
|
6
7
|
export function generateFetchWrapper(ctx) {
|
|
7
8
|
const ext = ctx.ext;
|
|
8
9
|
const ts = ext === "ts";
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const G = (type) => (ts ? `<${type}>` : "");
|
|
12
|
-
const tagInvalidation = ctx.config.features.tagInvalidation.enabled;
|
|
13
|
-
const authEnabled = ctx.config.features.auth.enabled;
|
|
14
|
-
const mutationQueue = ctx.config.features.mutationQueue.enabled;
|
|
15
|
-
const importLines = [
|
|
16
|
-
tagInvalidation
|
|
17
|
-
? `import { generateTags, invalidateUrl${tagInvalidation && mutationQueue ? ", expandCascading" : ""} } from "./invalidation-tags.${ext}";`
|
|
18
|
-
: "",
|
|
19
|
-
tagInvalidation ? `import { invalidateByTags } from "./cache.${ext}";` : "",
|
|
20
|
-
authEnabled
|
|
21
|
-
? `import { getAuth, clearAuth, withAuthHeaders, isAuthUrl, AUTH_WITH_CREDENTIALS } from "./auth/store.${ext}";`
|
|
22
|
-
: "",
|
|
23
|
-
mutationQueue
|
|
24
|
-
? `import { queueMutation } from "./mutation-queue.${ext}";`
|
|
25
|
-
: "",
|
|
26
|
-
]
|
|
27
|
-
.filter(Boolean)
|
|
28
|
-
.join("\n");
|
|
29
|
-
const interfaceBlock = ts
|
|
30
|
-
? `
|
|
31
|
-
export interface FetchWithCacheResult${G("T")} {
|
|
32
|
-
response${T("Response & { json(): Promise<T> }")};
|
|
33
|
-
fromCache${T("boolean")};
|
|
34
|
-
queued${T("boolean")};
|
|
35
|
-
}
|
|
36
|
-
`
|
|
37
|
-
: "";
|
|
38
|
-
const optionsInterface = ts
|
|
39
|
-
? `
|
|
40
|
-
export interface FetchWithCacheOptions extends RequestInit {
|
|
41
|
-
tags?: string[];
|
|
42
|
-
auth?: boolean;
|
|
43
|
-
queueOffline?: boolean;
|
|
44
|
-
invalidate?: 'auto' | string[] | false;
|
|
45
|
-
type?: 'read' | 'mutation';
|
|
46
|
-
strategy?: 'cache-first' | 'network-first' | 'stale-while-revalidate' | 'cache-only' | 'network-only' | 'reactive';
|
|
47
|
-
staleTime?: number;
|
|
48
|
-
/** Custom response validation for mutation success. Default: res.ok. Use this when your API returns 200 with { success: false } for logical failures. */
|
|
49
|
-
validateSuccess?: (response: Response) => boolean | Promise<boolean>;
|
|
50
|
-
/** Override the URL used for auto-invalidation. Defaults to the request URL. Useful when the mutation URL differs from the cache tag URL. */
|
|
51
|
-
invalidateUrl?: string;
|
|
52
|
-
}
|
|
53
|
-
`
|
|
54
|
-
: "";
|
|
55
|
-
const authBlock = authEnabled
|
|
56
|
-
? `
|
|
57
|
-
if (options.auth) {
|
|
58
|
-
const auth = await getAuth();
|
|
59
|
-
withAuthHeaders(headers, auth);
|
|
60
|
-
}`
|
|
61
|
-
: "";
|
|
62
|
-
const authUrlsBlock = authEnabled
|
|
63
|
-
? `
|
|
64
|
-
// Auth endpoints bypass SW cache
|
|
65
|
-
if (options.auth && isAuthUrl(url) && !headers.has("X-SW-Cache-Strategy")) {
|
|
66
|
-
headers.set("X-SW-Cache-Strategy", "mutation");
|
|
67
|
-
}`
|
|
68
|
-
: "";
|
|
69
|
-
const authCredentialsBlock = authEnabled
|
|
70
|
-
? `
|
|
71
|
-
if (AUTH_WITH_CREDENTIALS) {
|
|
72
|
-
fetchOptions.credentials = "include";
|
|
73
|
-
}`
|
|
74
|
-
: "";
|
|
75
|
-
const auth401Block = authEnabled
|
|
76
|
-
? `
|
|
77
|
-
if (options.auth && response.status === 401) {
|
|
78
|
-
await clearAuth();
|
|
79
|
-
window.dispatchEvent(new CustomEvent("sw-auth-unauthorized"));
|
|
80
|
-
}`
|
|
81
|
-
: "";
|
|
82
|
-
const mutationTagsBlock = tagInvalidation && mutationQueue
|
|
83
|
-
? ` // Pre-compute invalidation tags for SW-side IDB storage
|
|
84
|
-
let mutationTags: string[] = [];
|
|
85
|
-
if (!isRead) {
|
|
86
|
-
const invalidateSetting = options.invalidate !== false ? (options.invalidate || 'auto') : false;
|
|
87
|
-
if (invalidateSetting !== false) {
|
|
88
|
-
if (Array.isArray(invalidateSetting)) {
|
|
89
|
-
mutationTags = invalidateSetting;
|
|
90
|
-
} else {
|
|
91
|
-
mutationTags = generateTags(url);
|
|
92
|
-
mutationTags = expandCascading(mutationTags);
|
|
93
|
-
}
|
|
94
|
-
headers.set("X-SW-Invalidate-Tags", mutationTags.join(","));
|
|
95
|
-
}
|
|
96
|
-
}`
|
|
97
|
-
: ` let mutationTags: string[] = options.tags || [];`;
|
|
98
|
-
const offlineReadCatchBlock = ` if (options.signal?.aborted) throw new DOMException("The operation was aborted", "AbortError");
|
|
99
|
-
const cached = await caches.match(input);
|
|
100
|
-
if (cached) return { response: cached, fromCache: true, queued: false };
|
|
101
|
-
throw new Error("Offline: no cached data available");`;
|
|
102
|
-
const offlineWriteFallbackBlock = mutationQueue
|
|
103
|
-
? ` if (options.queueOffline !== false) {
|
|
104
|
-
await queueMutation({
|
|
105
|
-
method,
|
|
106
|
-
url,
|
|
107
|
-
body: options.body,
|
|
108
|
-
headers: {},
|
|
109
|
-
tags: mutationTags,
|
|
110
|
-
timestamp: Date.now(),
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
response: new Response(JSON.stringify({ queued: true }), { status: 202, headers: { "Content-Type": "application/json" } }),
|
|
114
|
-
fromCache: false,
|
|
115
|
-
queued: true,
|
|
116
|
-
};
|
|
117
|
-
}`
|
|
118
|
-
: ``;
|
|
119
|
-
const autoInvalidateBlock = tagInvalidation
|
|
120
|
-
? `
|
|
121
|
-
// Auto-invalidate after mutation success (skip if SW queued it)
|
|
122
|
-
const mutationSuccess = options.validateSuccess ? await options.validateSuccess(response) : response.ok;
|
|
123
|
-
const mutationQueued = response.headers.get("X-SW-Mutation-Queued") === "true";
|
|
124
|
-
if (!isRead && mutationSuccess && !mutationQueued) {
|
|
125
|
-
const invalidateSetting = options.invalidate !== false ? (options.invalidate || 'auto') : false;
|
|
126
|
-
if (invalidateSetting !== false) {
|
|
127
|
-
const invalidateTarget = options.invalidateUrl || url;
|
|
128
|
-
if (Array.isArray(invalidateSetting)) {
|
|
129
|
-
await invalidateByTags(invalidateSetting);
|
|
130
|
-
} else {
|
|
131
|
-
await invalidateUrl(invalidateTarget);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}`
|
|
135
|
-
: "";
|
|
136
|
-
const autoTagsBlock = tagInvalidation
|
|
137
|
-
? `
|
|
138
|
-
// Auto-generate tags from URL if not provided
|
|
139
|
-
if (!options.tags && isRead) {
|
|
140
|
-
const urlTags = generateTags(url);
|
|
141
|
-
if (urlTags.length > 0) {
|
|
142
|
-
headers.set("X-SW-Cache-Tags", urlTags.join(","));
|
|
143
|
-
}
|
|
144
|
-
}`
|
|
145
|
-
: "";
|
|
146
|
-
const code = `/**
|
|
147
|
-
* Swoff Fetch Wrapper
|
|
148
|
-
* Unified fetch with caching, auth, offline queue, auto-invalidation, staleTime, prefetching, and
|
|
149
|
-
* per-request strategy override.
|
|
150
|
-
*
|
|
151
|
-
* Usage:
|
|
152
|
-
* import { fetchWithCache, prefetchCache } from './swoff/fetch-wrapper.${ext}';
|
|
153
|
-
*
|
|
154
|
-
* // GET — cached with auto-generated tags
|
|
155
|
-
* const { response } = await fetchWithCache("/api/todos");
|
|
156
|
-
* const data = await response.json();
|
|
157
|
-
*
|
|
158
|
-
* // POST — mutation with auto-invalidation
|
|
159
|
-
* await fetchWithCache("/api/todos", {
|
|
160
|
-
* method: "POST",
|
|
161
|
-
* body: JSON.stringify({ title: "New task" }),
|
|
162
|
-
* });
|
|
163
|
-
*
|
|
164
|
-
* // Skip auto-invalidation, invalidate manually later
|
|
165
|
-
* await fetchWithCache("/api/todos", {
|
|
166
|
-
* method: "POST",
|
|
167
|
-
* body: JSON.stringify({ title: "New" }),
|
|
168
|
-
* invalidate: false,
|
|
169
|
-
* });
|
|
170
|
-
*
|
|
171
|
-
* // Validate mutation success with custom logic
|
|
172
|
-
* await fetchWithCache("/api/todos", {
|
|
173
|
-
* method: "POST",
|
|
174
|
-
* body: JSON.stringify({ title: "New" }),
|
|
175
|
-
* validateSuccess: (res) => res.status === 200,
|
|
176
|
-
* });
|
|
177
|
-
*
|
|
178
|
-
* // Custom tags for caching (read) + explicit invalidate tags (mutation)
|
|
179
|
-
* await fetchWithCache("/api/todos", {
|
|
180
|
-
* tags: ["custom-tag"],
|
|
181
|
-
* invalidate: ["custom-tag"],
|
|
182
|
-
* });
|
|
183
|
-
*
|
|
184
|
-
* // Authenticated request (works with bearer, cookie, custom)
|
|
185
|
-
* const { response: userRes } = await fetchWithCache("/api/me", { auth: true });
|
|
186
|
-
*
|
|
187
|
-
* // Prefetch (fire-and-forget warm the cache)
|
|
188
|
-
* prefetchCache("/api/todos");
|
|
189
|
-
*
|
|
190
|
-
* // Override caching strategy per-request (highest priority)
|
|
191
|
-
* await fetchWithCache("/api/checkout", {
|
|
192
|
-
* method: "POST",
|
|
193
|
-
* type: "read",
|
|
194
|
-
* strategy: "network-only",
|
|
195
|
-
* });
|
|
196
|
-
*
|
|
197
|
-
* // Support AbortController for cancellation
|
|
198
|
-
* const controller = new AbortController();
|
|
199
|
-
* setTimeout(() => controller.abort(), 5000);
|
|
200
|
-
* const { response } = await fetchWithCache("/api/todos", { signal: controller.signal });
|
|
201
|
-
*
|
|
202
|
-
* // Offline: auto-queues writes (disable with queueOffline: false)
|
|
203
|
-
* await fetchWithCache("/api/todos", {
|
|
204
|
-
* method: "POST",
|
|
205
|
-
* body: JSON.stringify({ title: "Offline task" }),
|
|
206
|
-
* });
|
|
207
|
-
* // When back online, processMutationQueue() replays them
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
${importLines}
|
|
211
|
-
${interfaceBlock}${optionsInterface}
|
|
212
|
-
const inFlightRequests = new Map${G("string, Promise<Response>")}();
|
|
213
|
-
|
|
214
|
-
/** Fetch with caching, auth, offline queue, auto-invalidation, and per-request strategy override. Returns { response, fromCache }. Use { auth: true } for authenticated requests — works with bearer, cookie, and custom auth types. */
|
|
215
|
-
export async function fetchWithCache${G("T")}(input${T("RequestInfo")}, options${T("RequestInit & FetchWithCacheOptions")} = {})${R("Promise<FetchWithCacheResult<T>>")}{
|
|
216
|
-
const method = (options.method || "GET").toUpperCase();
|
|
217
|
-
const isRead = options.type === "read" || (options.type !== "mutation" && (method === "GET" || method === "HEAD" || method === "OPTIONS"));
|
|
218
|
-
const url = typeof input === "string" ? input : input.url;
|
|
219
|
-
|
|
220
|
-
const headers = new Headers(options.headers);
|
|
221
|
-
|
|
222
|
-
// Set cache strategy
|
|
223
|
-
if (!headers.has("X-SW-Cache-Strategy")) {
|
|
224
|
-
headers.set("X-SW-Cache-Strategy", isRead ? "read" : "mutation");
|
|
225
|
-
}
|
|
226
|
-
${autoTagsBlock}
|
|
227
|
-
// Custom tags override auto-generated
|
|
228
|
-
if (options.tags && options.tags.length > 0) {
|
|
229
|
-
headers.set("X-SW-Cache-Tags", options.tags.join(","));
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (options.strategy) {
|
|
233
|
-
headers.set("X-SW-Strategy", options.strategy);
|
|
234
|
-
}
|
|
235
|
-
if (options.staleTime !== undefined) {
|
|
236
|
-
headers.set("X-SW-Stale-Time", String(options.staleTime));
|
|
237
|
-
}
|
|
238
|
-
${authBlock}${authUrlsBlock}
|
|
239
|
-
// Forward no-queue option to SW
|
|
240
|
-
if (options.queueOffline === false) {
|
|
241
|
-
headers.set("X-SW-No-Queue", "true");
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const fetchOptions${T("RequestInit")} = { ...options, headers };
|
|
245
|
-
${authCredentialsBlock}
|
|
246
|
-
|
|
247
|
-
// Check for abort before proceeding
|
|
248
|
-
if (options.signal?.aborted) {
|
|
249
|
-
throw new DOMException("The operation was aborted", "AbortError");
|
|
250
|
-
}
|
|
251
|
-
${mutationTagsBlock}
|
|
252
|
-
// Deduplicate in-flight GET requests
|
|
253
|
-
let responsePromise${T("Promise<Response>")};
|
|
254
|
-
if (isRead && inFlightRequests.has(url)) {
|
|
255
|
-
responsePromise = inFlightRequests.get(url)!.then((r) => r.clone());
|
|
256
|
-
} else {
|
|
257
|
-
const abortHandler = () => {
|
|
258
|
-
inFlightRequests.delete(url);
|
|
259
|
-
};
|
|
260
|
-
if (options.signal) {
|
|
261
|
-
options.signal.addEventListener("abort", abortHandler, { once: true });
|
|
262
|
-
}
|
|
263
|
-
responsePromise = fetch(input, fetchOptions);
|
|
264
|
-
if (isRead) {
|
|
265
|
-
const cleanup = () => {
|
|
266
|
-
inFlightRequests.delete(url);
|
|
267
|
-
if (options.signal) {
|
|
268
|
-
options.signal.removeEventListener("abort", abortHandler);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
inFlightRequests.set(url, responsePromise.finally(cleanup));
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
let response${T("Response")};
|
|
276
|
-
try {
|
|
277
|
-
response = await responsePromise;
|
|
278
|
-
} catch (err) {
|
|
279
|
-
if (err instanceof TypeError) {
|
|
280
|
-
if (isRead) {
|
|
281
|
-
${offlineReadCatchBlock}
|
|
282
|
-
}
|
|
283
|
-
// SW not controlling — fallback to client-side queue
|
|
284
|
-
if (!navigator.serviceWorker?.controller) {
|
|
285
|
-
${offlineWriteFallbackBlock}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
throw err;
|
|
289
|
-
}
|
|
290
|
-
${autoInvalidateBlock}${auth401Block}
|
|
291
|
-
const fromCache = response.headers.get("X-SW-From-Cache") === "true";
|
|
292
|
-
const queued = response.headers.get("X-SW-Mutation-Queued") === "true";
|
|
293
|
-
return { response, fromCache, queued };
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/** Fire-and-forget prefetch: warms the cache for a URL without blocking. Useful for route prefetching or link hover prefetching. */
|
|
297
|
-
export function prefetchCache(input${T("RequestInfo")}, options${T("RequestInit & FetchWithCacheOptions")} = {})${R("void")}{
|
|
298
|
-
fetchWithCache(input, { ...options }).catch(() => {
|
|
299
|
-
// Prefetch failures are intentionally silent
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
`;
|
|
303
|
-
writeFile(ctx, `fetch-wrapper.${ext}`, code);
|
|
10
|
+
const code = generateFetchWrapperCode({ ts, ext }, ctx.config.features.auth.enabled, ctx.config.features.auth.userEndpoint, ctx.config.features.mutationQueue.enabled, ctx.config.features.serviceWorker.requestBatchWindowMs);
|
|
11
|
+
writeFile(ctx, `fetch/core.${ext}`, code);
|
|
304
12
|
}
|
|
305
13
|
//# sourceMappingURL=fetch-wrapper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch-wrapper.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/fetch-wrapper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"fetch-wrapper.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/fetch-wrapper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAE7E,MAAM,UAAU,oBAAoB,CAAC,GAAqB;IACxD,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;IACpB,MAAM,EAAE,GAAG,GAAG,KAAK,IAAI,CAAC;IAExB,MAAM,IAAI,GAAG,wBAAwB,CACnC,EAAE,EAAE,EAAE,GAAG,EAAE,EACX,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAChC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,EACrC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,EACzC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,oBAAoB,CACvD,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE,cAAc,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync } from "fs";
|
|
2
|
+
import { join, dirname } from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
const templatesDir = join(__dirname, "../../../../templates");
|
|
6
|
+
function copyAdapter(frameworkDir, name, ext, outputDir) {
|
|
7
|
+
const src = join(frameworkDir, `${name}.${ext}x`);
|
|
8
|
+
if (existsSync(src)) {
|
|
9
|
+
copyFileSync(src, join(outputDir, `${name}.${ext}x`));
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const jsSrc = src.replace(`.${ext}x`, ".jsx");
|
|
13
|
+
if (existsSync(jsSrc)) {
|
|
14
|
+
copyFileSync(jsSrc, join(outputDir, `${name}.${ext}x`));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const ADAPTERS = [
|
|
19
|
+
{ name: "useNetworkStatus" },
|
|
20
|
+
{ name: "useCachedFetch" },
|
|
21
|
+
{ name: "useMutation" },
|
|
22
|
+
{ name: "usePrefetch" },
|
|
23
|
+
{ name: "useMutationState" },
|
|
24
|
+
{ name: "useCacheInvalidation" },
|
|
25
|
+
{ name: "useSwoffReset" },
|
|
26
|
+
{ name: "useIsFetching" },
|
|
27
|
+
{ name: "useSWUpdate", condition: (c) => c.config.features.pwa.enabled },
|
|
28
|
+
{ name: "useAuth", condition: (c) => c.config.features.auth.enabled },
|
|
29
|
+
{ name: "useMutationQueue", condition: (c) => c.config.features.mutationQueue.enabled },
|
|
30
|
+
{ name: "usePushSubscription", condition: (c) => c.config.features.pushNotifications?.enabled ?? false },
|
|
31
|
+
{ name: "useBackgroundSync", condition: (c) => !!c.config.features.backgroundSync },
|
|
32
|
+
];
|
|
33
|
+
const OUTPUT_SUBDIRS = {
|
|
34
|
+
react: "adapters",
|
|
35
|
+
vue: "adapters",
|
|
36
|
+
svelte: "adapters",
|
|
37
|
+
solid: "adapters",
|
|
38
|
+
};
|
|
39
|
+
export function generateFrameworkAdapters(ctx) {
|
|
40
|
+
const subdir = OUTPUT_SUBDIRS[ctx.frameworkName];
|
|
41
|
+
if (!subdir)
|
|
42
|
+
return;
|
|
43
|
+
const frameworkDir = join(templatesDir, ctx.frameworkName);
|
|
44
|
+
if (!existsSync(frameworkDir))
|
|
45
|
+
return;
|
|
46
|
+
const outDir = join(ctx.swoffDir, subdir);
|
|
47
|
+
if (!existsSync(outDir)) {
|
|
48
|
+
mkdirSync(outDir, { recursive: true });
|
|
49
|
+
}
|
|
50
|
+
for (const a of ADAPTERS) {
|
|
51
|
+
if (!a.condition || a.condition(ctx)) {
|
|
52
|
+
copyAdapter(frameworkDir, a.name, ctx.ext, outDir);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=generate-framework-adapters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-framework-adapters.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/generate-framework-adapters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAGpC,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1D,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,EAAE,uBAAuB,CAAC,CAAC;AAE9D,SAAS,WAAW,CAAC,YAAoB,EAAE,IAAY,EAAE,GAAW,EAAE,SAAiB;IACrF,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,GAAG,IAAI,IAAI,GAAG,GAAG,CAAC,CAAC;IAClD,IAAI,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACpB,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,IAAI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
39
|
+
copyHook(hooksDir, "useCacheInvalidation", ext);
|
|
40
|
+
// Phase 2 — always generated
|
|
41
|
+
copyHook(hooksDir, "useSwoffReset", ext);
|
|
42
|
+
copyHook(hooksDir, "useIsFetching", ext);
|
|
42
43
|
}
|
|
43
44
|
//# sourceMappingURL=generate-hooks.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate-hooks.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/generate-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAGpC,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1D,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,EAAE,6BAA6B,CAAC,CAAC;AAEpE,SAAS,QAAQ,CAAC,QAAgB,EAAE,IAAY,EAAE,GAAW;IAC3D,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,GAAG,IAAI,IAAI,GAAG,GAAG,CAAC,CAAC;IAClD,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,EAAE,GAAG,IAAI,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAqB;IACjD,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,aAAa,EAAE,GAAG,GAAG,CAAC;IACrD,IAAI,aAAa,KAAK,OAAO;QAAE,OAAO;IAEtC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,SAAS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,GAAG,CAAC,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,EAAE,GAAG,CAAC,CAAC;IAE5C,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QAChC,QAAQ,CAAC,QAAQ,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;IACzC,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QACjC,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;IACrC,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;QAC1C,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,CAAC,iBAAiB,EAAE,OAAO,EAAE,CAAC;QAC/C,QAAQ,CAAC,QAAQ,EAAE,qBAAqB,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC;QACnC,QAAQ,CAAC,QAAQ,EAAE,mBAAmB,EAAE,GAAG,CAAC,CAAC;IAC/C,CAAC;IACD,QAAQ,CAAC,QAAQ,EAAE,sBAAsB,EAAE,GAAG,CAAC,CAAC;IAEhD,6BAA6B;IAC7B,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,GAAG,CAAC,CAAC;IACzC,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,GAAG,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -1,139 +1,10 @@
|
|
|
1
1
|
import { writeFile } from "./context.js";
|
|
2
|
+
import { generateGqlWrapperCode } from "../../../runtime/gql-wrapper.js";
|
|
2
3
|
export function generateGqlWrapper(ctx) {
|
|
3
4
|
const ext = ctx.ext;
|
|
4
5
|
const ts = ext === "ts";
|
|
5
|
-
const T = (type) => (ts ? `: ${type}` : "");
|
|
6
|
-
const R = (type) => (ts ? `: ${type} ` : " ");
|
|
7
|
-
const G = (type) => (ts ? `<${type}>` : "");
|
|
8
|
-
const AS = (type) => (ts ? ` as ${type}` : "");
|
|
9
6
|
const endpoint = ctx.config.features.graphql.endpoint;
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
export interface GqlOptions {
|
|
13
|
-
variables?: Record<string, unknown>;
|
|
14
|
-
tags?: string[];
|
|
15
|
-
auth?: boolean;
|
|
16
|
-
queueOffline?: boolean;
|
|
17
|
-
invalidate?: 'auto' | string[] | false;
|
|
18
|
-
}
|
|
19
|
-
`
|
|
20
|
-
: "";
|
|
21
|
-
const code = `/**
|
|
22
|
-
* Swoff GraphQL Wrapper
|
|
23
|
-
* Brings Swoff's caching, offline queue, and tag-based invalidation to GraphQL APIs.
|
|
24
|
-
* Hashes query + variables for deterministic cache keys and auto-generates tags
|
|
25
|
-
* from operation names. Built on top of fetchWithCache.
|
|
26
|
-
*
|
|
27
|
-
* Usage:
|
|
28
|
-
* import { fetchWithGql, queryGql, mutateGql } from './swoff/gql-wrapper.${ext}';
|
|
29
|
-
*
|
|
30
|
-
* // Query — cached with body-hash key
|
|
31
|
-
* const { data } = await queryGql("{ todos { id title } }");
|
|
32
|
-
*
|
|
33
|
-
* // Query with variables
|
|
34
|
-
* const { data: todo } = await queryGql(
|
|
35
|
-
* "query GetTodo($id: ID!) { todo(id: $id) { id title } }",
|
|
36
|
-
* { id: "42" }
|
|
37
|
-
* );
|
|
38
|
-
*
|
|
39
|
-
* // Mutation — auto-invalidates related cache tags
|
|
40
|
-
* const { data: created } = await mutateGql(
|
|
41
|
-
* "mutation CreateTodo($title: String!) { createTodo(title: $title) { id } }",
|
|
42
|
-
* { title: "New task" }
|
|
43
|
-
* );
|
|
44
|
-
*
|
|
45
|
-
* // Authenticated
|
|
46
|
-
* const { data } = await queryGql("query Me { me { name } }", {}, { auth: true });
|
|
47
|
-
*
|
|
48
|
-
* // Offline: mutations are auto-queued
|
|
49
|
-
* const { data } = await mutateGql(
|
|
50
|
-
* "mutation CreateTodo($title: String!) { createTodo(title: $title) { id } }",
|
|
51
|
-
* { title: "Offline task" },
|
|
52
|
-
* { queueOffline: true }
|
|
53
|
-
* );
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
import { fetchWithCache } from "./fetch-wrapper.${ext}";
|
|
57
|
-
import type { GqlResult } from "./swoff";
|
|
58
|
-
${optionsInterface}
|
|
59
|
-
/** Extract operation name from a GraphQL document. Returns null for anonymous queries (e.g. "{ todos { id } }"). */
|
|
60
|
-
function getOperationName(query${T("string")})${T("string | null")}{
|
|
61
|
-
const match = query.match(/(query|mutation|subscription)\\s+(\\w+)/);
|
|
62
|
-
return match ? match[2] : null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Determine if a GraphQL document is a query (read) vs mutation/subscription (write). */
|
|
66
|
-
function isReadOperation(query${T("string")})${T("boolean")}{
|
|
67
|
-
const trimmed = query.trim();
|
|
68
|
-
if (trimmed.startsWith("mutation") || trimmed.startsWith("subscription")) return false;
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Hash an object into a hex string for deterministic cache keying. Uses SHA-256 via SubtleCrypto. */
|
|
73
|
-
async function bodyHash(obj${T("unknown")})${T("Promise<string>")}{
|
|
74
|
-
const json = JSON.stringify(obj);
|
|
75
|
-
const bytes = new TextEncoder().encode(json);
|
|
76
|
-
const hash = await crypto.subtle.digest("SHA-256", bytes);
|
|
77
|
-
return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Generate cache-relevant tags from a GQL operation name. e.g. "getTodos" → ["todos"], "createTodo" → ["todos", "todo"]. */
|
|
81
|
-
function tagsFromOpName(name${T("string | null")})${T("string[]")}{
|
|
82
|
-
if (!name) return [];
|
|
83
|
-
const stripped = name.replace(/^(get|fetch|list|all|query)/i, "").replace(/^(create|set|add|new|update|delete|remove)/i, "");
|
|
84
|
-
if (!stripped) return [name.toLowerCase()];
|
|
85
|
-
const tag = stripped.charAt(0).toLowerCase() + stripped.slice(1);
|
|
86
|
-
const plural = tag.replace(/s$/, "") + "s";
|
|
87
|
-
return [plural, tag];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Fetch a GraphQL endpoint with Swoff's caching, auth, offline queue, and auto-invalidation. Hashes query + variables for deterministic cache keys. */
|
|
91
|
-
export async function fetchWithGql${G("T")}(
|
|
92
|
-
query${T("string")},
|
|
93
|
-
options${T("GqlOptions")} = {}
|
|
94
|
-
)${R("Promise<GqlResult<T>>")}{
|
|
95
|
-
const isRead = isReadOperation(query);
|
|
96
|
-
const opName = getOperationName(query);
|
|
97
|
-
const variables = options.variables;
|
|
98
|
-
const hash = await bodyHash({ query, variables });
|
|
99
|
-
const tags = options.tags || tagsFromOpName(opName);
|
|
100
|
-
|
|
101
|
-
const { response, fromCache } = await fetchWithCache("${endpoint}", {
|
|
102
|
-
method: "POST",
|
|
103
|
-
body: JSON.stringify({ query, variables }),
|
|
104
|
-
headers: {
|
|
105
|
-
"Content-Type": "application/json",
|
|
106
|
-
"X-SW-Cache-Key": "gql:" + hash,
|
|
107
|
-
},
|
|
108
|
-
tags,
|
|
109
|
-
type: isRead ? "read" : "mutation",
|
|
110
|
-
auth: options.auth,
|
|
111
|
-
queueOffline: options.queueOffline,
|
|
112
|
-
invalidate: options.invalidate,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
const result = await response.json();
|
|
116
|
-
return { data: result.data${AS("T")}, fromCache };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Shorthand for GraphQL queries (type: "read"). Cached by default with body-hash key. */
|
|
120
|
-
export async function queryGql${G("T")}(
|
|
121
|
-
query${T("string")},
|
|
122
|
-
variables${T("Record<string, unknown> | undefined")} = undefined,
|
|
123
|
-
options${T("GqlOptions")} = {}
|
|
124
|
-
)${R("Promise<GqlResult<T>>")}{
|
|
125
|
-
return fetchWithGql${G("T")}(query, { ...options, variables });
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Shorthand for GraphQL mutations (type: "mutation"). Auto-invalidates related cache tags. */
|
|
129
|
-
export async function mutateGql${G("T")}(
|
|
130
|
-
mutation${T("string")},
|
|
131
|
-
variables${T("Record<string, unknown> | undefined")} = undefined,
|
|
132
|
-
options${T("GqlOptions")} = {}
|
|
133
|
-
)${R("Promise<GqlResult<T>>")}{
|
|
134
|
-
return fetchWithGql${G("T")}(mutation, { ...options, variables });
|
|
135
|
-
}
|
|
136
|
-
`;
|
|
137
|
-
writeFile(ctx, `gql-wrapper.${ext}`, code);
|
|
7
|
+
const code = generateGqlWrapperCode({ ts, ext }, endpoint);
|
|
8
|
+
writeFile(ctx, `graphql/index.${ext}`, code);
|
|
138
9
|
}
|
|
139
10
|
//# sourceMappingURL=gql-wrapper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gql-wrapper.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/gql-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"gql-wrapper.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/gql-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAEzE,MAAM,UAAU,kBAAkB,CAAC,GAAqB;IACtD,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;IACpB,MAAM,EAAE,GAAG,GAAG,KAAK,IAAI,CAAC;IACxB,MAAM,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC;IAEtD,MAAM,IAAI,GAAG,sBAAsB,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IAE3D,SAAS,CAAC,GAAG,EAAE,iBAAiB,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Generates GUIDE.md — points user to online documentation.
|
|
2
|
+
* Generates GUIDE.md — points user to online documentation and PWA asset info.
|
|
3
3
|
*/
|
|
4
4
|
import { writeFile } from "./context.js";
|
|
5
5
|
export function generateGuide(ctx) {
|
|
@@ -9,6 +9,34 @@ export function generateGuide(ctx) {
|
|
|
9
9
|
w("");
|
|
10
10
|
w("Your project was generated with Swoff. All generated files live in `swoff/`.");
|
|
11
11
|
w("");
|
|
12
|
+
if (ctx.config.features.pwa.enabled && ctx.config.features.pwa.assets.source) {
|
|
13
|
+
w("## PWA Assets");
|
|
14
|
+
w("");
|
|
15
|
+
w("PWA assets (icons, favicon, OG image, splash screens) will be generated from your");
|
|
16
|
+
w(` source logo during \`swoff generate\`.`);
|
|
17
|
+
w("");
|
|
18
|
+
w("To regenerate manually:");
|
|
19
|
+
w("");
|
|
20
|
+
w(" swoff assets --source <path>");
|
|
21
|
+
w("");
|
|
22
|
+
w("### What gets generated");
|
|
23
|
+
w("");
|
|
24
|
+
w("- PWA icons: 64×64, 192×192, 512×512, maskable 512×512");
|
|
25
|
+
w("- Apple touch icon: 180×180");
|
|
26
|
+
w("- Apple splash screens: all device resolutions");
|
|
27
|
+
w("- Favicon: SVG + ICO (16×16, 32×32, 48×48)");
|
|
28
|
+
w("- OG image: 1200×630 (also used for Twitter card)");
|
|
29
|
+
w("");
|
|
30
|
+
w("After generation, reference these assets in your app manually. Run \`swoff assets\` for a copy-paste guide with the exact <link> and <meta> tags.");
|
|
31
|
+
w("");
|
|
32
|
+
w("### Source logo requirements");
|
|
33
|
+
w("");
|
|
34
|
+
w("- **SVG** recommended — produces the best quality at all sizes");
|
|
35
|
+
w("- PNG or JPG accepted — will be upscaled if needed");
|
|
36
|
+
w("- Minimum 512×512 for crisp icons");
|
|
37
|
+
w("- Transparent backgrounds work well with theme colors");
|
|
38
|
+
w("");
|
|
39
|
+
}
|
|
12
40
|
w("For the full documentation, visit:");
|
|
13
41
|
w("");
|
|
14
42
|
w("- **Documentation** — https://swoff.dev/docs");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guide-generator.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/guide-generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;AAE3D,MAAM,UAAU,aAAa,CAAC,GAAqB;IACjD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,CAAC,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEvC,CAAC,CAAC,2BAA2B,CAAC,CAAC;IAC/B,CAAC,CAAC,EAAE,CAAC,CAAC;IACN,CAAC,CACC,8EAA8E,CAC/E,CAAC;IACF,CAAC,CAAC,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"guide-generator.js","sourceRoot":"","sources":["../../../../src/lib/generators/file-generators/guide-generator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAoB,SAAS,EAAE,MAAM,cAAc,CAAC;AAE3D,MAAM,UAAU,aAAa,CAAC,GAAqB;IACjD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,CAAC,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEvC,CAAC,CAAC,2BAA2B,CAAC,CAAC;IAC/B,CAAC,CAAC,EAAE,CAAC,CAAC;IACN,CAAC,CACC,8EAA8E,CAC/E,CAAC;IACF,CAAC,CAAC,EAAE,CAAC,CAAC;IAEN,IAAI,GAAG,CAAC,MAAM,CAAC,QAA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