@swoff/cli 0.3.13 → 0.3.15
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 +28 -32
- package/dist/index.js +4 -15
- package/dist/index.js.map +1 -1
- package/dist/lib/cli/help.js +4 -9
- package/dist/lib/cli/help.js.map +1 -1
- package/dist/lib/commands/clean.js +8 -17
- package/dist/lib/commands/clean.js.map +1 -1
- package/dist/lib/commands/generate-guide.js +16 -30
- package/dist/lib/commands/generate-guide.js.map +1 -1
- package/dist/lib/commands/generate.js +33 -7
- package/dist/lib/commands/generate.js.map +1 -1
- package/dist/lib/commands/init.js +259 -23
- package/dist/lib/commands/init.js.map +1 -1
- package/dist/lib/commands/validate.js +2 -4
- package/dist/lib/commands/validate.js.map +1 -1
- package/dist/lib/config/loader.js +34 -25
- package/dist/lib/config/loader.js.map +1 -1
- package/dist/lib/config/minimal-config.js +64 -0
- package/dist/lib/config/minimal-config.js.map +1 -0
- package/dist/lib/config/validator.js +219 -110
- package/dist/lib/config/validator.js.map +1 -1
- package/dist/lib/generators/file-generators/api-config.js +1 -1
- package/dist/lib/generators/file-generators/api-config.js.map +1 -1
- package/dist/lib/generators/file-generators/auth-adapter.js +13 -0
- package/dist/lib/generators/file-generators/auth-adapter.js.map +1 -0
- package/dist/lib/generators/file-generators/auth-check.js +40 -0
- package/dist/lib/generators/file-generators/auth-check.js.map +1 -0
- package/dist/lib/generators/file-generators/auth-store.js +2 -5
- package/dist/lib/generators/file-generators/auth-store.js.map +1 -1
- package/dist/lib/generators/file-generators/background-sync.js +1 -1
- package/dist/lib/generators/file-generators/background-sync.js.map +1 -1
- package/dist/lib/generators/file-generators/cache.js +2 -1
- package/dist/lib/generators/file-generators/cache.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector-bundle.js +15 -0
- package/dist/lib/generators/file-generators/client-injector-bundle.js.map +1 -0
- package/dist/lib/generators/file-generators/client-injector.js +2 -1
- package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/connectivity.js +12 -0
- package/dist/lib/generators/file-generators/connectivity.js.map +1 -0
- package/dist/lib/generators/file-generators/context.js +7 -0
- package/dist/lib/generators/file-generators/context.js.map +1 -1
- package/dist/lib/generators/file-generators/fetch-wrapper.js +1 -1
- package/dist/lib/generators/file-generators/fetch-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/generate-framework-adapters.js +29 -20
- package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -1
- package/dist/lib/generators/file-generators/gql-wrapper.js +2 -2
- package/dist/lib/generators/file-generators/gql-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/guide-generator.js +51 -1
- package/dist/lib/generators/file-generators/guide-generator.js.map +1 -1
- package/dist/lib/generators/file-generators/invalidation-tags.js +2 -3
- package/dist/lib/generators/file-generators/invalidation-tags.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-queue.js +2 -2
- package/dist/lib/generators/file-generators/mutation-queue.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-state.js +1 -1
- package/dist/lib/generators/file-generators/mutation-state.js.map +1 -1
- package/dist/lib/generators/file-generators/open-db.js +6 -0
- package/dist/lib/generators/file-generators/open-db.js.map +1 -0
- package/dist/lib/generators/file-generators/push.js +1 -2
- package/dist/lib/generators/file-generators/push.js.map +1 -1
- package/dist/lib/generators/file-generators/pwa-install.js +2 -6
- package/dist/lib/generators/file-generators/pwa-install.js.map +1 -1
- package/dist/lib/generators/file-generators/server-push.js +1 -1
- package/dist/lib/generators/file-generators/server-push.js.map +1 -1
- package/dist/lib/generators/file-generators/storage.js +6 -0
- package/dist/lib/generators/file-generators/storage.js.map +1 -0
- package/dist/lib/generators/file-generators/sw-generator-build.js +105 -48
- package/dist/lib/generators/file-generators/sw-generator-build.js.map +1 -1
- package/dist/lib/generators/file-generators/sw-injector.js +0 -4
- package/dist/lib/generators/file-generators/sw-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/sw-template.js +8 -9
- package/dist/lib/generators/file-generators/sw-template.js.map +1 -1
- package/dist/lib/generators/file-generators/swoff-api-bundle.js +51 -0
- package/dist/lib/generators/file-generators/swoff-api-bundle.js.map +1 -0
- package/dist/lib/generators/sw-build-utils.js +69 -0
- package/dist/lib/generators/sw-build-utils.js.map +1 -0
- package/dist/lib/generators/sw-generator.js +23 -34
- package/dist/lib/generators/sw-generator.js.map +1 -1
- package/dist/lib/generators/sw-sections/activate-handler.js +77 -23
- package/dist/lib/generators/sw-sections/activate-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/assemble-sw.js +20 -95
- package/dist/lib/generators/sw-sections/assemble-sw.js.map +1 -1
- package/dist/lib/generators/sw-sections/background-precache.js +126 -0
- package/dist/lib/generators/sw-sections/background-precache.js.map +1 -0
- package/dist/lib/generators/sw-sections/background-sync-handler.js +74 -72
- package/dist/lib/generators/sw-sections/background-sync-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js +102 -0
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js.map +1 -0
- package/dist/lib/generators/sw-sections/config-header.js +2 -3
- package/dist/lib/generators/sw-sections/config-header.js.map +1 -1
- package/dist/lib/generators/sw-sections/default-template.js +64 -7
- package/dist/lib/generators/sw-sections/default-template.js.map +1 -1
- package/dist/lib/generators/sw-sections/fetch-handler.js +621 -495
- package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/install-handler.js +41 -23
- package/dist/lib/generators/sw-sections/install-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/message-handler.js +34 -8
- package/dist/lib/generators/sw-sections/message-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/server-push-handler.js +10 -14
- package/dist/lib/generators/sw-sections/server-push-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/shared.js +70 -0
- package/dist/lib/generators/sw-sections/shared.js.map +1 -0
- package/dist/lib/generators/sw-sections/sw-push.js +2 -6
- package/dist/lib/generators/sw-sections/sw-push.js.map +1 -1
- package/dist/lib/generators/sw-sections/tag-management.js +55 -86
- package/dist/lib/generators/sw-sections/tag-management.js.map +1 -1
- package/dist/lib/generators/swoff-files-generator.js +183 -59
- package/dist/lib/generators/swoff-files-generator.js.map +1 -1
- package/dist/lib/shared/config-types.js +84 -79
- package/dist/lib/shared/config-types.js.map +1 -1
- package/dist/lib/shared/feature-registry.js +198 -0
- package/dist/lib/shared/feature-registry.js.map +1 -0
- package/dist/lib/utils/detect-framework.js +52 -9
- package/dist/lib/utils/detect-framework.js.map +1 -1
- package/dist/runtime/api-config.js +2 -2
- package/dist/runtime/api-config.js.map +1 -1
- package/dist/runtime/auth-adapter.js +179 -0
- package/dist/runtime/auth-adapter.js.map +1 -0
- package/dist/runtime/auth-state.js +16 -6
- package/dist/runtime/auth-state.js.map +1 -1
- package/dist/runtime/auth-store.js +219 -213
- package/dist/runtime/auth-store.js.map +1 -1
- package/dist/runtime/background-sync.js +5 -2
- package/dist/runtime/background-sync.js.map +1 -1
- package/dist/runtime/cache.js +91 -3
- package/dist/runtime/cache.js.map +1 -1
- package/dist/runtime/client-injector-bundle.js +314 -0
- package/dist/runtime/client-injector-bundle.js.map +1 -0
- package/dist/runtime/client-injector.js +188 -30
- package/dist/runtime/client-injector.js.map +1 -1
- package/dist/runtime/connectivity.js +83 -0
- package/dist/runtime/connectivity.js.map +1 -0
- package/dist/runtime/fetch-wrapper.js +26 -59
- package/dist/runtime/fetch-wrapper.js.map +1 -1
- package/dist/runtime/gql-wrapper.js +15 -9
- package/dist/runtime/gql-wrapper.js.map +1 -1
- package/dist/runtime/invalidation-tags.js +12 -89
- package/dist/runtime/invalidation-tags.js.map +1 -1
- package/dist/runtime/mutation-queue.js +83 -31
- package/dist/runtime/mutation-queue.js.map +1 -1
- package/dist/runtime/mutation-state.js +3 -3
- package/dist/runtime/mutation-state.js.map +1 -1
- package/dist/runtime/open-db.js +41 -0
- package/dist/runtime/open-db.js.map +1 -0
- package/dist/runtime/push.js +15 -27
- package/dist/runtime/push.js.map +1 -1
- package/dist/runtime/pwa-prompt.js +35 -3
- package/dist/runtime/pwa-prompt.js.map +1 -1
- package/dist/runtime/reset.js +39 -42
- package/dist/runtime/reset.js.map +1 -1
- package/dist/runtime/server-push.js +23 -13
- package/dist/runtime/server-push.js.map +1 -1
- package/dist/runtime/storage.js +32 -0
- package/dist/runtime/storage.js.map +1 -0
- package/dist/runtime/sw-injector.js +19 -178
- package/dist/runtime/sw-injector.js.map +1 -1
- package/dist/runtime/swoff-api-bundle.js +1491 -0
- package/dist/runtime/swoff-api-bundle.js.map +1 -0
- package/dist/runtime/type-definitions.js +8 -7
- package/dist/runtime/type-definitions.js.map +1 -1
- package/dist/runtime/utils.js +4 -0
- package/dist/runtime/utils.js.map +1 -1
- package/package.json +9 -3
- package/templates/react/useAuth.jsx +7 -4
- package/templates/react/useAuth.tsx +14 -14
- package/templates/react/useBackgroundSync.jsx +5 -4
- package/templates/react/useBackgroundSync.tsx +5 -4
- package/templates/react/useCachedFetch.jsx +8 -0
- package/templates/react/useCachedFetch.tsx +8 -0
- package/templates/react/useMutation.jsx +60 -56
- package/templates/react/useMutation.tsx +65 -64
- package/templates/react/useMutationQueue.jsx +5 -1
- package/templates/react/useMutationQueue.tsx +8 -2
- package/templates/react/useMutationState.jsx +3 -4
- package/templates/react/useMutationState.tsx +4 -8
- package/templates/react/useNetworkStatus.jsx +57 -17
- package/templates/react/useNetworkStatus.tsx +42 -20
- package/templates/react/useOfflineAnalytics.jsx +42 -0
- package/templates/react/useOfflineAnalytics.tsx +50 -0
- package/templates/react/usePwaInstall.jsx +26 -0
- package/templates/react/usePwaInstall.tsx +26 -0
- package/templates/react/useSWUpdate.jsx +7 -52
- package/templates/react/useSWUpdate.tsx +12 -51
- package/templates/react/useStorageEstimate.jsx +56 -0
- package/templates/react/useStorageEstimate.tsx +67 -0
- package/dist/__tests__/assemble-sw.test.js +0 -145
- package/dist/__tests__/assemble-sw.test.js.map +0 -1
- package/dist/__tests__/cli-integration.test.js +0 -231
- package/dist/__tests__/cli-integration.test.js.map +0 -1
- package/dist/__tests__/config-types.test.js +0 -124
- package/dist/__tests__/config-types.test.js.map +0 -1
- package/dist/__tests__/detect-language.test.js +0 -51
- package/dist/__tests__/detect-language.test.js.map +0 -1
- package/dist/__tests__/file-generators.test.js +0 -228
- package/dist/__tests__/file-generators.test.js.map +0 -1
- package/dist/__tests__/glob-matcher.test.js +0 -86
- package/dist/__tests__/glob-matcher.test.js.map +0 -1
- package/dist/__tests__/invalidation-tags.test.js +0 -283
- package/dist/__tests__/invalidation-tags.test.js.map +0 -1
- package/dist/__tests__/loader.test.js +0 -77
- package/dist/__tests__/loader.test.js.map +0 -1
- package/dist/__tests__/validator.test.js +0 -340
- package/dist/__tests__/validator.test.js.map +0 -1
- package/dist/lib/commands/add.js +0 -120
- package/dist/lib/commands/add.js.map +0 -1
- package/dist/lib/commands/assets.js +0 -45
- package/dist/lib/commands/assets.js.map +0 -1
- package/dist/lib/commands/generate-assets.js +0 -44
- package/dist/lib/commands/generate-assets.js.map +0 -1
- package/dist/lib/commands/info.js +0 -183
- package/dist/lib/commands/info.js.map +0 -1
- package/dist/lib/generators/asset-generator/dependency-resolver.js +0 -85
- package/dist/lib/generators/asset-generator/dependency-resolver.js.map +0 -1
- package/dist/lib/generators/asset-generator/generate.js +0 -111
- package/dist/lib/generators/asset-generator/generate.js.map +0 -1
- package/dist/lib/generators/asset-generator/guide.js +0 -43
- package/dist/lib/generators/asset-generator/guide.js.map +0 -1
- package/dist/lib/generators/asset-generator/html-patcher.js +0 -65
- package/dist/lib/generators/asset-generator/html-patcher.js.map +0 -1
- package/dist/lib/generators/asset-generator/ico-encoder.js +0 -29
- package/dist/lib/generators/asset-generator/ico-encoder.js.map +0 -1
- package/dist/lib/generators/asset-generator/manifest-patcher.js +0 -23
- package/dist/lib/generators/asset-generator/manifest-patcher.js.map +0 -1
- package/dist/lib/generators/asset-generator/maskable.js +0 -15
- package/dist/lib/generators/asset-generator/maskable.js.map +0 -1
- package/dist/lib/generators/asset-generator/rasterize.js +0 -32
- package/dist/lib/generators/asset-generator/rasterize.js.map +0 -1
- package/dist/lib/generators/asset-generator/sizes.js +0 -25
- package/dist/lib/generators/asset-generator/sizes.js.map +0 -1
- package/dist/lib/generators/file-generators/auth-fetch.js +0 -151
- package/dist/lib/generators/file-generators/auth-fetch.js.map +0 -1
- package/dist/lib/generators/file-generators/auth-user.js +0 -13
- package/dist/lib/generators/file-generators/auth-user.js.map +0 -1
- package/dist/lib/generators/file-generators/fetch-state.js +0 -6
- package/dist/lib/generators/file-generators/fetch-state.js.map +0 -1
- package/dist/lib/generators/file-generators/generate-hooks.js +0 -44
- package/dist/lib/generators/file-generators/generate-hooks.js.map +0 -1
- package/dist/lib/generators/file-generators/indexeddb.js +0 -74
- package/dist/lib/generators/file-generators/indexeddb.js.map +0 -1
- package/dist/lib/generators/file-generators/manifest.js +0 -46
- package/dist/lib/generators/file-generators/manifest.js.map +0 -1
- package/dist/lib/generators/file-generators/mutation-reconcile.js +0 -57
- package/dist/lib/generators/file-generators/mutation-reconcile.js.map +0 -1
- package/dist/lib/generators/file-generators/quick-readme.js +0 -21
- package/dist/lib/generators/file-generators/quick-readme.js.map +0 -1
- package/dist/lib/generators/file-generators/reconcile.js +0 -50
- package/dist/lib/generators/file-generators/reconcile.js.map +0 -1
- package/dist/lib/generators/file-generators/store.js +0 -86
- package/dist/lib/generators/file-generators/store.js.map +0 -1
- package/dist/lib/generators/file-generators/sw-push.js +0 -67
- package/dist/lib/generators/file-generators/sw-push.js.map +0 -1
- package/dist/lib/generators/sw-sections/glob-matcher-code.js.map +0 -1
- package/dist/lib/utils/build-script.js +0 -35
- package/dist/lib/utils/build-script.js.map +0 -1
- package/dist/lib/utils/tty-status.js +0 -21
- package/dist/lib/utils/tty-status.js.map +0 -1
- package/dist/runtime/auth-user.js +0 -168
- package/dist/runtime/auth-user.js.map +0 -1
- package/dist/runtime/fetch-state.js +0 -42
- package/dist/runtime/fetch-state.js.map +0 -1
- package/dist/runtime/pwa-index.js +0 -11
- package/dist/runtime/pwa-index.js.map +0 -1
- package/dist/runtime/pwa-injector.js +0 -44
- package/dist/runtime/pwa-injector.js.map +0 -1
- package/dist/runtime/pwa-install.js +0 -83
- package/dist/runtime/pwa-install.js.map +0 -1
- package/templates/react/useCacheInvalidation.jsx +0 -19
- package/templates/react/useCacheInvalidation.tsx +0 -52
- package/templates/react/useIsFetching.jsx +0 -32
- package/templates/react/useIsFetching.tsx +0 -32
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { T, R } from "./utils.js";
|
|
2
|
+
function generateCookieAdapter(ts, ext) {
|
|
3
|
+
return `/**
|
|
4
|
+
* Auth Adapter — maps Swoff auth infrastructure to your auth provider.
|
|
5
|
+
*
|
|
6
|
+
* Cookie (session) auth: the server manages sessions via an httpOnly cookie.
|
|
7
|
+
* The browser auto-sends the cookie on every same-origin request, so
|
|
8
|
+
* no header injection is needed.
|
|
9
|
+
*
|
|
10
|
+
* Edit the URLs below if your backend uses different endpoints.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* import { adapter } from "./auth/adapter.${ext}";
|
|
14
|
+
* const headers = adapter.getHeaders(auth);
|
|
15
|
+
*/${ts ? `
|
|
16
|
+
// ─── AuthData — single source of truth for auth shapes ───────────────
|
|
17
|
+
// Edit the user type to match your backend's user object.
|
|
18
|
+
export interface AuthData {
|
|
19
|
+
token?: string;
|
|
20
|
+
user?: unknown;
|
|
21
|
+
expiresAt?: number;
|
|
22
|
+
}
|
|
23
|
+
` : ""}
|
|
24
|
+
import { isAuthFailureResponse } from "./check.${ext}";
|
|
25
|
+
|
|
26
|
+
export const adapter${T(ts, "{ type: 'cookie'; getAuth: () => Promise<AuthData | null>; getHeaders: (auth: AuthData | null) => Record<string, string>; refresh: (auth: AuthData) => Promise<AuthData | null>; fetchUser: () => Promise<AuthData | null> }")}= {
|
|
27
|
+
type: "cookie",
|
|
28
|
+
|
|
29
|
+
/** Get current auth state from your provider. Return null if not authenticated. */
|
|
30
|
+
async getAuth()${R(ts, "Promise<AuthData | null>")}{
|
|
31
|
+
return null;
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/** Generate auth headers for fetch requests. Cookie auth returns empty — browser sends cookie. */
|
|
35
|
+
getHeaders(_auth${T(ts, "AuthData | null")})${R(ts, "Record<string, string>")}{
|
|
36
|
+
return {};
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
/** Refresh the session. Cookie auth: server manages session, return null to skip. */
|
|
40
|
+
async refresh(_auth${T(ts, "AuthData")})${R(ts, "Promise<AuthData | null>")}{
|
|
41
|
+
return null;
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
/** Fetch current user from the server. Uses credentials: include for cookie auth. */
|
|
45
|
+
async fetchUser()${R(ts, "Promise<AuthData | null>")}{
|
|
46
|
+
const res = await fetch("/api/me", { credentials: "include" });
|
|
47
|
+
if (await isAuthFailureResponse(res)) return null;
|
|
48
|
+
return { user: await res.json() };
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
function generateBearerAdapter(ts, ext) {
|
|
54
|
+
return `/**
|
|
55
|
+
* Auth Adapter — maps Swoff auth infrastructure to your auth provider.
|
|
56
|
+
*
|
|
57
|
+
* Bearer token auth: the server returns a token on login. The client stores it
|
|
58
|
+
* in memory and sends it as \`Authorization: Bearer <token>\` on every request.
|
|
59
|
+
*
|
|
60
|
+
* Edit the URLs below if your backend uses different endpoints.
|
|
61
|
+
*
|
|
62
|
+
* Usage:
|
|
63
|
+
* import { adapter } from "./auth/adapter.${ext}";
|
|
64
|
+
* const headers = adapter.getHeaders(auth);
|
|
65
|
+
* const refreshed = await adapter.refresh(auth);
|
|
66
|
+
*/${ts ? `
|
|
67
|
+
// ─── AuthData — single source of truth for auth shapes ───────────────
|
|
68
|
+
// Edit the user type to match your backend's user object.
|
|
69
|
+
export interface AuthData {
|
|
70
|
+
token?: string;
|
|
71
|
+
user?: unknown;
|
|
72
|
+
expiresAt?: number;
|
|
73
|
+
}
|
|
74
|
+
` : ""}
|
|
75
|
+
import { getAuth } from "./store.${ext}";
|
|
76
|
+
import { isAuthFailureResponse } from "./check.${ext}";
|
|
77
|
+
|
|
78
|
+
export const adapter${T(ts, "{ type: 'bearer'; getAuth: () => Promise<AuthData | null>; getHeaders: (auth: AuthData | null) => Record<string, string>; refresh: (auth: AuthData) => Promise<AuthData | null>; fetchUser: () => Promise<AuthData | null> }")}= {
|
|
79
|
+
type: "bearer",
|
|
80
|
+
|
|
81
|
+
/** Get current auth state from your provider. Return null if not authenticated. */
|
|
82
|
+
async getAuth()${R(ts, "Promise<AuthData | null>")}{
|
|
83
|
+
return null;
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/** Generate auth headers. Injects Bearer token if available. */
|
|
87
|
+
getHeaders(auth${T(ts, "AuthData | null")})${R(ts, "Record<string, string>")}{
|
|
88
|
+
return auth?.token ? { Authorization: \`Bearer \${auth.token}\` } : {};
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
/** Refresh the session. Sends the existing token to /api/refresh. */
|
|
92
|
+
async refresh(auth${T(ts, "AuthData")})${R(ts, "Promise<AuthData | null>")}{
|
|
93
|
+
try {
|
|
94
|
+
const headers${T(ts, "Record<string, string>")} = { "Content-Type": "application/json" };
|
|
95
|
+
if (auth.token) headers["Authorization"] = \`Bearer \${auth.token}\`;
|
|
96
|
+
const res = await fetch("/api/refresh", { method: "POST", headers });
|
|
97
|
+
if (await isAuthFailureResponse(res)) return null;
|
|
98
|
+
const data = await res.json();
|
|
99
|
+
return { ...auth, token: data.token, expiresAt: data.expiresAt };
|
|
100
|
+
} catch { return null; }
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
/** Fetch current user from the server. Injects Bearer token if available. */
|
|
104
|
+
async fetchUser()${R(ts, "Promise<AuthData | null>")}{
|
|
105
|
+
const auth = await getAuth();
|
|
106
|
+
const headers${T(ts, "Record<string, string>")} = {};
|
|
107
|
+
if (auth?.token) headers["Authorization"] = \`Bearer \${auth.token}\`;
|
|
108
|
+
const res = await fetch("/api/me", { headers });
|
|
109
|
+
if (await isAuthFailureResponse(res)) return null;
|
|
110
|
+
return { user: await res.json() };
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
function generateCustomAdapter(ts, ext) {
|
|
116
|
+
return `/**
|
|
117
|
+
* Auth Adapter — maps Swoff auth infrastructure to your auth provider.
|
|
118
|
+
*
|
|
119
|
+
* EDIT THIS FILE to match your backend's auth flow.
|
|
120
|
+
*
|
|
121
|
+
* Usage:
|
|
122
|
+
* import { adapter } from "./auth/adapter.${ext}";
|
|
123
|
+
* const headers = adapter.getHeaders(auth);
|
|
124
|
+
*/${ts ? `
|
|
125
|
+
// ─── AuthData — single source of truth for auth shapes ───────────────
|
|
126
|
+
// Edit all fields to match your backend's auth shapes.
|
|
127
|
+
export interface AuthData {
|
|
128
|
+
token?: string;
|
|
129
|
+
user?: unknown;
|
|
130
|
+
expiresAt?: number;
|
|
131
|
+
}
|
|
132
|
+
` : ""}
|
|
133
|
+
import { isAuthFailureResponse } from "./check.${ext}";
|
|
134
|
+
|
|
135
|
+
export const adapter = {
|
|
136
|
+
/** Set to "cookie" if your backend uses httpOnly cookies, "bearer" if it uses tokens. */
|
|
137
|
+
type: "bearer",
|
|
138
|
+
|
|
139
|
+
/** Get current auth state. Implement if your provider has a getSession() equivalent. */
|
|
140
|
+
async getAuth() { return null; },
|
|
141
|
+
|
|
142
|
+
/** Generate auth headers for fetch requests. */
|
|
143
|
+
getHeaders(auth) {
|
|
144
|
+
// --- EDIT THIS ---
|
|
145
|
+
if (auth?.token) return { Authorization: \`Bearer \${auth.token}\` };
|
|
146
|
+
return {};
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/** Refresh the session. Return null if refresh fails. Use isAuthFailureResponse to validate. */
|
|
150
|
+
async refresh(auth) {
|
|
151
|
+
// --- EDIT THIS ---
|
|
152
|
+
try {
|
|
153
|
+
const headers = { "Content-Type": "application/json" };
|
|
154
|
+
if (auth?.token) headers["Authorization"] = \`Bearer \${auth.token}\`;
|
|
155
|
+
const res = await fetch("/api/refresh", { method: "POST", headers });
|
|
156
|
+
if (await isAuthFailureResponse(res)) return null;
|
|
157
|
+
const data = await res.json();
|
|
158
|
+
return { ...auth, token: data.token, expiresAt: data.expiresAt };
|
|
159
|
+
} catch { return null; }
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
/** Fetch current user. Return AuthData | null (e.g. { user: data }). Use isAuthFailureResponse to validate. */
|
|
163
|
+
async fetchUser() {
|
|
164
|
+
// --- EDIT THIS ---
|
|
165
|
+
return null;
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
`;
|
|
169
|
+
}
|
|
170
|
+
const ADAPTER_GENERATORS = {
|
|
171
|
+
"cookie": generateCookieAdapter,
|
|
172
|
+
"bearer": generateBearerAdapter,
|
|
173
|
+
"custom": generateCustomAdapter,
|
|
174
|
+
};
|
|
175
|
+
export function generateAuthAdapterCode(ctx, authType) {
|
|
176
|
+
const generator = ADAPTER_GENERATORS[authType] || ADAPTER_GENERATORS["custom"];
|
|
177
|
+
return generator(ctx.ts, ctx.ext);
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=auth-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-adapter.js","sourceRoot":"","sources":["../../src/runtime/auth-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAElC,SAAS,qBAAqB,CAAC,EAAW,EAAE,GAAW;IACrD,OAAO;;;;;;;;;;+CAUsC,GAAG;;KAE7C,EAAE,CAAC,CAAC,CAAC;;;;;;;;CAQT,CAAC,CAAC,CAAC,EAAE;iDAC2C,GAAG;;sBAE9B,CAAC,CAAC,EAAE,EAAE,8NAA8N,CAAC;;;;mBAIxO,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;;;;oBAKhC,CAAC,CAAC,EAAE,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,wBAAwB,CAAC;;;;;uBAKxD,CAAC,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;;;;qBAKxD,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;;;;;CAMrD,CAAC;AACF,CAAC;AAED,SAAS,qBAAqB,CAAC,EAAW,EAAE,GAAW;IACrD,OAAO;;;;;;;;;+CASsC,GAAG;;;KAG7C,EAAE,CAAC,CAAC,CAAC;;;;;;;;CAQT,CAAC,CAAC,CAAC,EAAE;mCAC6B,GAAG;iDACW,GAAG;;sBAE9B,CAAC,CAAC,EAAE,EAAE,8NAA8N,CAAC;;;;mBAIxO,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;;;;mBAKjC,CAAC,CAAC,EAAE,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,wBAAwB,CAAC;;;;;sBAKxD,CAAC,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;qBAEvD,CAAC,CAAC,EAAE,EAAE,wBAAwB,CAAC;;;;;;;;;;qBAU/B,CAAC,CAAC,EAAE,EAAE,0BAA0B,CAAC;;mBAEnC,CAAC,CAAC,EAAE,EAAE,wBAAwB,CAAC;;;;;;;CAOjD,CAAC;AACF,CAAC;AAED,SAAS,qBAAqB,CAAC,EAAW,EAAE,GAAW;IACrD,OAAO;;;;;;+CAMsC,GAAG;;KAE7C,EAAE,CAAC,CAAC,CAAC;;;;;;;;CAQT,CAAC,CAAC,CAAC,EAAE;iDAC2C,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmCnD,CAAC;AACF,CAAC;AAED,MAAM,kBAAkB,GAAyD;IAC/E,QAAQ,EAAE,qBAAqB;IAC/B,QAAQ,EAAE,qBAAqB;IAC/B,QAAQ,EAAE,qBAAqB;CAChC,CAAC;AAEF,MAAM,UAAU,uBAAuB,CACrC,GAAmB,EACnB,QAAgB;IAEhB,MAAM,SAAS,GAAG,kBAAkB,CAAC,QAAQ,CAAC,IAAI,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IAC/E,OAAO,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -10,24 +10,34 @@ export function generateAuthStateCode(ctx) {
|
|
|
10
10
|
* 3. Offline + Authenticated → Offline access (cached data)
|
|
11
11
|
* 4. Offline + Unauthenticated → Strict offline (public content only)
|
|
12
12
|
*
|
|
13
|
+
* getAuth() tries the adapter first, then falls back to IndexedDB user cache,
|
|
14
|
+
* so this works offline when the provider is unreachable.
|
|
15
|
+
*
|
|
16
|
+
* Public API:
|
|
17
|
+
* getAuthState — detect the 4-state matrix (online/offline × auth/unauthed)
|
|
18
|
+
*
|
|
19
|
+
* Internal imports (from ./store):
|
|
20
|
+
* getAuth, isAuthValid — used internally, not re-exported
|
|
21
|
+
*
|
|
13
22
|
* Usage:
|
|
14
23
|
* import { getAuthState } from "./auth/state.${ext}";
|
|
15
|
-
* const { authenticated,
|
|
24
|
+
* const { authenticated, auth, online } = await getAuthState();
|
|
25
|
+
* const userName = auth?.user?.name; // typed via AuthData
|
|
16
26
|
*/
|
|
17
27
|
|
|
28
|
+
import { getCurrentOnlineStatus } from "../connectivity.${ext}";
|
|
18
29
|
import { getAuth, isAuthValid } from "./store.${ext}";
|
|
19
|
-
import {
|
|
30
|
+
import type { AuthData } from "./adapter.${ext}";
|
|
20
31
|
|
|
21
32
|
/** Detect current auth state across the 4-state matrix (online/offline × authenticated/not). */
|
|
22
|
-
export async function getAuthState()${R(ts, "Promise<{ authenticated: boolean;
|
|
33
|
+
export async function getAuthState()${R(ts, "Promise<{ authenticated: boolean; auth: AuthData | null; online: boolean }>")}{
|
|
23
34
|
const auth = await getAuth();
|
|
24
35
|
const valid = isAuthValid(auth);
|
|
25
|
-
const user = valid ? await getCachedUser() : null;
|
|
26
36
|
|
|
27
37
|
return {
|
|
28
38
|
authenticated: valid,
|
|
29
|
-
|
|
30
|
-
online:
|
|
39
|
+
auth,
|
|
40
|
+
online: getCurrentOnlineStatus(),
|
|
31
41
|
};
|
|
32
42
|
}
|
|
33
43
|
`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-state.js","sourceRoot":"","sources":["../../src/runtime/auth-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAE/B,MAAM,UAAU,qBAAqB,CAAC,GAAmB;IACvD,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,OAAO
|
|
1
|
+
{"version":3,"file":"auth-state.js","sourceRoot":"","sources":["../../src/runtime/auth-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAE/B,MAAM,UAAU,qBAAqB,CAAC,GAAmB;IACvD,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,OAAO;;;;;;;;;;;;;;;;;;;kDAmByC,GAAG;;;;;0DAKK,GAAG;gDACb,GAAG;2CACR,GAAG;;;sCAGR,CAAC,CAAC,EAAE,EAAE,6EAA6E,CAAC;;;;;;;;;;CAUzH,CAAC;AACF,CAAC"}
|