create-ailk 0.36.0 → 0.37.0
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/component-catalog.json +40 -1
- package/component-catalog.md +7 -5
- package/dist/surfaces.js +60 -19
- package/dist/update.d.ts +19 -1
- package/dist/update.js +293 -35
- package/package.json +3 -3
- package/templates/.env.example +17 -0
- package/templates/apps/api/.env.example +38 -0
- package/templates/apps/api/CLAUDE.md +3 -0
- package/templates/apps/api/src/lib/__mocks__/prisma.ts +7 -0
- package/templates/apps/mcp/CLAUDE.md +1 -1
- package/templates/apps/web/CLAUDE.md +28 -1
- package/templates/apps/web/app/[locale]/(auth)/reset-password/__tests__/return.test.tsx +179 -0
- package/templates/apps/web/app/[locale]/(auth)/reset-password/page.tsx +22 -2
- package/templates/apps/web/app/[locale]/(auth)/reset-password/reset-password-card.tsx +9 -2
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/billing.test.tsx +240 -76
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/general.test.tsx +242 -28
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/hub-mode.test.tsx +369 -196
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/item.test.tsx +26 -1
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/layout.test.tsx +17 -6
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/new-menu.test.tsx +51 -12
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/new-popups.test.tsx +17 -2
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/notifications.test.tsx +153 -2
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/page.test.tsx +206 -23
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/popups.test.tsx +150 -188
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/privacy.test.tsx +235 -177
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/rich-translator.ts +63 -0
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/security.test.tsx +0 -3
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/thread.test.tsx +340 -0
- package/templates/apps/web/app/[locale]/(authed)/account/account-data.ts +7 -5
- package/templates/apps/web/app/[locale]/(authed)/account/account-frame.tsx +19 -3
- package/templates/apps/web/app/[locale]/(authed)/account/actions.ts +8 -4
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/[slug]/page.tsx +161 -0
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/__tests__/page.test.tsx +426 -0
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/appointments-data.ts +76 -0
- package/templates/apps/web/app/[locale]/(authed)/account/appointments/page.tsx +139 -0
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/page.tsx +92 -24
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/[pieceId]/versions/[version]/file/route.ts +32 -1
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/__tests__/file-route.test.ts +76 -3
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/__tests__/page.test.tsx +290 -41
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/artifact-data.ts +109 -11
- package/templates/apps/web/app/[locale]/(authed)/account/artifacts/page.tsx +43 -44
- package/templates/apps/web/app/[locale]/(authed)/account/billing/__tests__/offerings.test.ts +99 -0
- package/templates/apps/web/app/[locale]/(authed)/account/billing/offerings.ts +123 -0
- package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +309 -113
- package/templates/apps/web/app/[locale]/(authed)/account/billing-source.ts +192 -65
- package/templates/apps/web/app/[locale]/(authed)/account/components/request-quote.tsx +52 -7
- package/templates/apps/web/app/[locale]/(authed)/account/general/account-name.ts +36 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/contact-actions.ts +70 -2
- package/templates/apps/web/app/[locale]/(authed)/account/general/contact-details.tsx +42 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +55 -58
- package/templates/apps/web/app/[locale]/(authed)/account/general/phone-verification.tsx +2 -1
- package/templates/apps/web/app/[locale]/(authed)/account/general/photo-actions.ts +73 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/profile-form.tsx +12 -9
- package/templates/apps/web/app/[locale]/(authed)/account/general/profile-source.ts +18 -4
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/[kind]/[id]/page.tsx +20 -2
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/member-thread-composer.tsx +43 -0
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/page.tsx +125 -0
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/thread-data.ts +88 -0
- package/templates/apps/web/app/[locale]/(authed)/account/inbox/thread/thread-write.ts +31 -0
- package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +3 -2
- package/templates/apps/web/app/[locale]/(authed)/account/new-menu.tsx +33 -8
- package/templates/apps/web/app/[locale]/(authed)/account/new-popups.tsx +31 -52
- package/templates/apps/web/app/[locale]/(authed)/account/notifications/page.tsx +44 -4
- package/templates/apps/web/app/[locale]/(authed)/account/page.tsx +69 -49
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/__tests__/export-route.test.ts +173 -0
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/actions.ts +42 -4
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/export/route.ts +102 -0
- package/templates/apps/web/app/[locale]/(authed)/account/privacy/page.tsx +147 -137
- package/templates/apps/web/app/[locale]/(authed)/account/security/__tests__/hub-variant.test.tsx +258 -0
- package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +18 -21
- package/templates/apps/web/app/[locale]/(authed)/account/security/security-panel.tsx +34 -6
- package/templates/apps/web/app/[locale]/(authed)/account/security/security-source.ts +37 -7
- package/templates/apps/web/app/[locale]/(authed)/account/settings/page.tsx +0 -2
- package/templates/apps/web/app/[locale]/(authed)/account/settings-frame.tsx +8 -5
- package/templates/apps/web/app/[locale]/flows/[slug]/results/[payload]/ResultsClient.tsx +3 -2
- package/templates/apps/web/app/api/auth/hub/password/__tests__/return.test.ts +86 -0
- package/templates/apps/web/app/api/auth/hub/password/__tests__/route.test.ts +368 -0
- package/templates/apps/web/app/api/auth/hub/password/return.ts +86 -0
- package/templates/apps/web/app/api/auth/hub/password/route.ts +171 -0
- package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +263 -0
- package/templates/apps/web/app/api/hub-session/password/return/route.ts +69 -0
- package/templates/apps/web/app/api/hub-session/password/route.ts +106 -0
- package/templates/apps/web/lib/__tests__/account-config.test.ts +129 -1
- package/templates/apps/web/lib/__tests__/offerings-pricing.test.ts +33 -0
- package/templates/apps/web/lib/account-config.ts +132 -8
- package/templates/apps/web/lib/auth-routes.ts +11 -6
- package/templates/apps/web/lib/offerings-pricing.ts +4 -1
- package/templates/apps/web/messages/en.json +156 -80
- package/versions.json +48 -48
- package/templates/apps/web/app/[locale]/(authed)/account/components/schedule-service.tsx +0 -276
- package/templates/apps/web/app/[locale]/(authed)/account/components/send-message.tsx +0 -128
- package/templates/apps/web/app/[locale]/(authed)/account/settings/__tests__/theme-modes.test.ts +0 -42
- package/templates/apps/web/app/[locale]/(authed)/account/settings/theme-modes.ts +0 -40
package/templates/apps/web/app/[locale]/(authed)/account/privacy/__tests__/export-route.test.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The person's data export, re-served (#11164 AC-3) —
|
|
7
|
+
* `GET /[locale]/account/privacy/export`.
|
|
8
|
+
*
|
|
9
|
+
* The api-client's `member.exportData` is the one external boundary mocked;
|
|
10
|
+
* `forwardedCookieInit()` and the route's decisions are real (the build's
|
|
11
|
+
* own sign-in, a single site: the caller's session cookie). Covers:
|
|
12
|
+
* - the read carries the caller's credential and the JSON comes back with
|
|
13
|
+
* all five hub headers unchanged;
|
|
14
|
+
* - a hub answer missing any of the five is refused 502, its stream
|
|
15
|
+
* cancelled — never bytes on this origin without them;
|
|
16
|
+
* - a cross-site navigation is refused 403 before any call;
|
|
17
|
+
* - refusals map to a status with no body and `private, no-store`: a 4xx
|
|
18
|
+
* keeps its status (the 429 when the person's hourly exports are spent),
|
|
19
|
+
* a 5xx or a throw is 502, no credential is 401.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const mockExportData = jest.fn();
|
|
23
|
+
jest.mock("@working-theory/api-client", () => ({
|
|
24
|
+
__esModule: true,
|
|
25
|
+
member: { exportData: (...args: unknown[]) => mockExportData(...args) },
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
const mockHeaders = jest.fn();
|
|
29
|
+
const mockCookies = jest.fn();
|
|
30
|
+
jest.mock("next/headers", () => ({
|
|
31
|
+
__esModule: true,
|
|
32
|
+
headers: (...args: unknown[]) => mockHeaders(...args),
|
|
33
|
+
cookies: (...args: unknown[]) => mockCookies(...args),
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
import { GET as route } from "../export/route";
|
|
37
|
+
|
|
38
|
+
/** A same-origin navigation to the route, unless `site` says otherwise. */
|
|
39
|
+
const GET = (site: string | null = "same-origin") =>
|
|
40
|
+
route(
|
|
41
|
+
new Request("https://site.example.com/account/privacy/export", {
|
|
42
|
+
headers: site === null ? {} : { "sec-fetch-site": site },
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const FIVE = {
|
|
47
|
+
contentType: "application/json; charset=utf-8",
|
|
48
|
+
contentDisposition: 'attachment; filename="your-data.json"',
|
|
49
|
+
contentTypeOptions: "nosniff",
|
|
50
|
+
contentSecurityPolicy:
|
|
51
|
+
"default-src 'none'; sandbox; frame-ancestors 'none'; form-action 'none'; base-uri 'none'",
|
|
52
|
+
cacheControl: "private, no-store",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
mockExportData.mockReset();
|
|
57
|
+
mockHeaders
|
|
58
|
+
.mockReset()
|
|
59
|
+
.mockResolvedValue(new Headers({ origin: "https://site.example.com" }));
|
|
60
|
+
mockCookies.mockReset().mockResolvedValue({ toString: () => "session=abc" });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("reads the export with the caller's own credential and re-serves it with all five headers unchanged", async () => {
|
|
64
|
+
const stream = new ReadableStream<Uint8Array>();
|
|
65
|
+
mockExportData.mockResolvedValue({
|
|
66
|
+
ok: true,
|
|
67
|
+
body: { stream, headers: FIVE },
|
|
68
|
+
});
|
|
69
|
+
const res = await GET();
|
|
70
|
+
expect(mockExportData).toHaveBeenCalledWith({
|
|
71
|
+
headers: { cookie: "session=abc", origin: "https://site.example.com" },
|
|
72
|
+
});
|
|
73
|
+
expect(res.status).toBe(200);
|
|
74
|
+
expect(res.body).toBe(stream);
|
|
75
|
+
expect(res.headers.get("Content-Type")).toBe(FIVE.contentType);
|
|
76
|
+
expect(res.headers.get("Content-Disposition")).toBe(FIVE.contentDisposition);
|
|
77
|
+
expect(res.headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
78
|
+
expect(res.headers.get("Content-Security-Policy")).toBe(
|
|
79
|
+
FIVE.contentSecurityPolicy,
|
|
80
|
+
);
|
|
81
|
+
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it.each(Object.keys(FIVE) as (keyof typeof FIVE)[])(
|
|
85
|
+
"a hub answer missing %s is refused 502 with the stream cancelled",
|
|
86
|
+
async (missing) => {
|
|
87
|
+
const cancel = jest.fn().mockResolvedValue(undefined);
|
|
88
|
+
mockExportData.mockResolvedValue({
|
|
89
|
+
ok: true,
|
|
90
|
+
body: { stream: { cancel }, headers: { ...FIVE, [missing]: null } },
|
|
91
|
+
});
|
|
92
|
+
const res = await GET();
|
|
93
|
+
expect(res.status).toBe(502);
|
|
94
|
+
expect(res.body).toBeNull();
|
|
95
|
+
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
|
|
96
|
+
expect(cancel).toHaveBeenCalledTimes(1);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const refusal = (status: number, code: string) => ({
|
|
101
|
+
ok: false,
|
|
102
|
+
status,
|
|
103
|
+
body: { ok: false, error: { code, message: code } },
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it.each([
|
|
107
|
+
[
|
|
108
|
+
"a 429 (the hourly budget spent) answers 429",
|
|
109
|
+
refusal(429, "rate_limited"),
|
|
110
|
+
429,
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
"a 403 (email unverified) answers 403",
|
|
114
|
+
refusal(403, "email_unverified"),
|
|
115
|
+
403,
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
"a 404 (an older hub with no export route) answers 404",
|
|
119
|
+
refusal(404, "not_found"),
|
|
120
|
+
404,
|
|
121
|
+
],
|
|
122
|
+
["a 503 from the hub answers 502", refusal(503, "tenant_unresolved"), 502],
|
|
123
|
+
["no credential (signed out) answers 401", refusal(0, "unauthorized"), 401],
|
|
124
|
+
[
|
|
125
|
+
"a transport the api-client refuses answers 502",
|
|
126
|
+
refusal(0, "insecure_transport"),
|
|
127
|
+
502,
|
|
128
|
+
],
|
|
129
|
+
])("%s with no body and private, no-store", async (_name, answer, status) => {
|
|
130
|
+
mockExportData.mockResolvedValue(answer);
|
|
131
|
+
const res = await GET();
|
|
132
|
+
expect(res.status).toBe(status);
|
|
133
|
+
expect(res.body).toBeNull();
|
|
134
|
+
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("a 429 keeps the hub's retry hint as Retry-After, rebuilt from the refusal's retryAfterSeconds", async () => {
|
|
138
|
+
mockExportData.mockResolvedValue({
|
|
139
|
+
ok: false,
|
|
140
|
+
status: 429,
|
|
141
|
+
body: { error: "rate_limited", retryAfterSeconds: 1800 },
|
|
142
|
+
});
|
|
143
|
+
const res = await GET();
|
|
144
|
+
expect(res.status).toBe(429);
|
|
145
|
+
expect(res.headers.get("Retry-After")).toBe("1800");
|
|
146
|
+
expect(res.body).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it.each(["cross-site", "same-site"])(
|
|
150
|
+
"a %s navigation is refused 403 before any call",
|
|
151
|
+
async (site) => {
|
|
152
|
+
const res = await GET(site);
|
|
153
|
+
expect(res.status).toBe(403);
|
|
154
|
+
expect(res.body).toBeNull();
|
|
155
|
+
expect(mockExportData).not.toHaveBeenCalled();
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
it("a navigation with no Sec-Fetch-Site (an older browser) and a `none` one (the address bar) are served", async () => {
|
|
160
|
+
mockExportData.mockResolvedValue({
|
|
161
|
+
ok: true,
|
|
162
|
+
body: { stream: new ReadableStream<Uint8Array>(), headers: FIVE },
|
|
163
|
+
});
|
|
164
|
+
expect((await GET(null)).status).toBe(200);
|
|
165
|
+
expect((await GET("none")).status).toBe(200);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("a hub call that throws is a 502, never Next's own 500", async () => {
|
|
169
|
+
mockExportData.mockRejectedValue(new Error("ECONNREFUSED"));
|
|
170
|
+
const res = await GET();
|
|
171
|
+
expect(res.status).toBe(502);
|
|
172
|
+
expect(res.body).toBeNull();
|
|
173
|
+
});
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* A/privacy/actions.ts — the member Privacy and Notifications tabs' one
|
|
5
5
|
* consent write path (#8438 D14, D15, spec
|
|
6
|
-
* docs/specs/8438-consent-registry-and-ledger-step-spec.md)
|
|
6
|
+
* docs/specs/8438-consent-registry-and-ledger-step-spec.md), and the Privacy
|
|
7
|
+
* tab's stored-sessions deletion (#11164 AC-3).
|
|
7
8
|
*
|
|
8
9
|
* `setConsentAction` posts one answer through `POST /v1/member/consents`
|
|
9
10
|
* (`member.setConsent`) and answers in `ConsentSwitchList`'s own shape, so
|
|
@@ -12,18 +13,36 @@
|
|
|
12
13
|
* when present, linking its cookie choices to the client record this write
|
|
13
14
|
* resolves or creates (D6, D11, D12) — never failing the write when it is
|
|
14
15
|
* absent or invalid.
|
|
16
|
+
*
|
|
17
|
+
* `deleteStoredSessionsAction` posts `POST /v1/member/stored-sessions/delete`
|
|
18
|
+
* (`member.deleteStoredSessions`) with the caller's own credential and
|
|
19
|
+
* answers in `AccountDataTable`'s shape: the line to show — the count, or
|
|
20
|
+
* that there was nothing to delete — or the failure message. The
|
|
21
|
+
* confirmation is the table's; this runs only after it.
|
|
15
22
|
*/
|
|
16
23
|
|
|
17
24
|
import { member } from "@working-theory/api-client";
|
|
18
|
-
import type {
|
|
25
|
+
import type {
|
|
26
|
+
AccountDataActionResult,
|
|
27
|
+
ConsentSwitchChangeResult,
|
|
28
|
+
} from "@working-theory/templates/account";
|
|
29
|
+
import {
|
|
30
|
+
formatLocalTime,
|
|
31
|
+
SERVER_TIME_ZONE,
|
|
32
|
+
} from "@working-theory/ui/primitives";
|
|
19
33
|
import { cookies } from "next/headers";
|
|
20
34
|
import { getLocale, getTranslations } from "next-intl/server";
|
|
21
35
|
|
|
22
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
consentVisitorHeader,
|
|
38
|
+
readConsentVisitor,
|
|
39
|
+
} from "../../../../../lib/consent-visitor";
|
|
23
40
|
import { forwardedCookieInit } from "../../../../../lib/server-api";
|
|
24
41
|
|
|
42
|
+
// #11159: the server fallback zone, never the host's, which the unpinned
|
|
43
|
+
// form read; the line is a `t()` string, so no node can carry the viewer's.
|
|
25
44
|
function formatDate(iso: string, locale: string): string {
|
|
26
|
-
return
|
|
45
|
+
return formatLocalTime(iso, "date", { timeZone: SERVER_TIME_ZONE, locale });
|
|
27
46
|
}
|
|
28
47
|
|
|
29
48
|
export async function setConsentAction(
|
|
@@ -63,3 +82,22 @@ export async function setConsentAction(
|
|
|
63
82
|
return { ok: false, message: t("settings.privacy.unavailable") };
|
|
64
83
|
}
|
|
65
84
|
}
|
|
85
|
+
|
|
86
|
+
export async function deleteStoredSessionsAction(): Promise<AccountDataActionResult> {
|
|
87
|
+
const t = await getTranslations("account");
|
|
88
|
+
try {
|
|
89
|
+
const res = await member.deleteStoredSessions(await forwardedCookieInit());
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
return {
|
|
92
|
+
ok: false,
|
|
93
|
+
message: t("settings.privacy.yourData.deleteFailed"),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
line: t("settings.privacy.yourData.deleted", { count: res.body.deleted }),
|
|
99
|
+
};
|
|
100
|
+
} catch {
|
|
101
|
+
return { ok: false, message: t("settings.privacy.yourData.deleteFailed") };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The person's data export, re-served (#11164 AC-3) —
|
|
3
|
+
* `GET /[locale]/account/privacy/export`.
|
|
4
|
+
*
|
|
5
|
+
* The Privacy tab's "Export data" button links here. The browser holds
|
|
6
|
+
* neither the client's hub credential (sealed in the `hub-session` cookie)
|
|
7
|
+
* nor this site's spoke key, so this route reads `GET /v1/member/data-export`
|
|
8
|
+
* with `forwardedCookieInit()` — the caller's own credential, so a person
|
|
9
|
+
* only ever gets their own records — and streams the JSON back unread.
|
|
10
|
+
*
|
|
11
|
+
* The same contract as the member file reads (`inbox/message/[id]/file`):
|
|
12
|
+
* the bytes go out with all five hub headers UNCHANGED — `Content-Type`,
|
|
13
|
+
* `Content-Disposition` (attachment, `your-data.json`), `X-Content-Type-Options:
|
|
14
|
+
* nosniff`, the sandbox `Content-Security-Policy` and `Cache-Control: private,
|
|
15
|
+
* no-store` — so the file is never a document on this origin and never cached
|
|
16
|
+
* on the way. A hub answer missing any of the five is refused 502 rather than
|
|
17
|
+
* served without it.
|
|
18
|
+
*
|
|
19
|
+
* A refusal has no body: a cross-site navigation (`Sec-Fetch-Site` other
|
|
20
|
+
* than `same-origin` or `none` — a third-party link must not spend the
|
|
21
|
+
* person's hourly exports or drop a file on their disk) is a 403 before any
|
|
22
|
+
* call; no credential (signed out) is a 401, a hub 4xx keeps its status (a
|
|
23
|
+
* 429 when the person's hourly exports are spent), and a hub 5xx, a
|
|
24
|
+
* transport the api-client refuses or a hub call that throws is a 502.
|
|
25
|
+
*/
|
|
26
|
+
import { member, type MemberFileHeaders } from "@working-theory/api-client";
|
|
27
|
+
|
|
28
|
+
import { forwardedCookieInit } from "../../../../../../lib/server-api";
|
|
29
|
+
|
|
30
|
+
/** The response header each of the five hub headers goes out under. */
|
|
31
|
+
const FILE_HEADER_NAMES: Record<keyof MemberFileHeaders, string> = {
|
|
32
|
+
contentType: "Content-Type",
|
|
33
|
+
contentDisposition: "Content-Disposition",
|
|
34
|
+
contentTypeOptions: "X-Content-Type-Options",
|
|
35
|
+
contentSecurityPolicy: "Content-Security-Policy",
|
|
36
|
+
cacheControl: "Cache-Control",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function refusal(status: number, retryAfterSeconds?: number): Response {
|
|
40
|
+
return new Response(null, {
|
|
41
|
+
status,
|
|
42
|
+
headers: {
|
|
43
|
+
"Cache-Control": "private, no-store",
|
|
44
|
+
// A 429 keeps the hub's retry hint: the api-client answers the JSON
|
|
45
|
+
// body, not the headers, and the API's refusal names the seconds.
|
|
46
|
+
...(retryAfterSeconds === undefined
|
|
47
|
+
? {}
|
|
48
|
+
: { "Retry-After": String(retryAfterSeconds) }),
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The API's `retryAfterSeconds`, when the refusal body carries one. */
|
|
54
|
+
function retryAfterOf(body: unknown): number | undefined {
|
|
55
|
+
const seconds = (body as { retryAfterSeconds?: unknown } | null)
|
|
56
|
+
?.retryAfterSeconds;
|
|
57
|
+
return typeof seconds === "number" && Number.isFinite(seconds)
|
|
58
|
+
? seconds
|
|
59
|
+
: undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The refusal's status, from the api-client's `{ ok: false, status, body }`. */
|
|
63
|
+
function refusalStatus(status: number, body: unknown): number {
|
|
64
|
+
if (status === 0) {
|
|
65
|
+
const code = (body as { error?: { code?: unknown } } | null)?.error?.code;
|
|
66
|
+
return code === "unauthorized" ? 401 : 502;
|
|
67
|
+
}
|
|
68
|
+
return status >= 500 ? 502 : status;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// platform-decisions-waiver: api-versioning — a page-scoped download link on the site's own account area, not an API contract; it re-serves the versioned hub route /v1/member/data-export
|
|
72
|
+
export async function GET(request: Request): Promise<Response> {
|
|
73
|
+
const site = request.headers.get("sec-fetch-site");
|
|
74
|
+
if (site !== null && site !== "same-origin" && site !== "none")
|
|
75
|
+
return refusal(403);
|
|
76
|
+
|
|
77
|
+
// The api-client throws, rather than answering `{ ok: false }`, on a
|
|
78
|
+
// network failure or a redirect — that is a 502 too, never Next's own 500.
|
|
79
|
+
let res: Awaited<ReturnType<typeof member.exportData>>;
|
|
80
|
+
try {
|
|
81
|
+
res = await member.exportData(await forwardedCookieInit());
|
|
82
|
+
} catch {
|
|
83
|
+
return refusal(502);
|
|
84
|
+
}
|
|
85
|
+
if (!res.ok)
|
|
86
|
+
return refusal(refusalStatus(res.status, res.body), retryAfterOf(res.body));
|
|
87
|
+
|
|
88
|
+
const { stream, headers } = res.body;
|
|
89
|
+
const out = new Headers();
|
|
90
|
+
for (const [key, name] of Object.entries(FILE_HEADER_NAMES) as [
|
|
91
|
+
keyof MemberFileHeaders,
|
|
92
|
+
string,
|
|
93
|
+
][]) {
|
|
94
|
+
const value = headers[key];
|
|
95
|
+
if (value === null) {
|
|
96
|
+
await stream.cancel();
|
|
97
|
+
return refusal(502);
|
|
98
|
+
}
|
|
99
|
+
out.set(name, value);
|
|
100
|
+
}
|
|
101
|
+
return new Response(stream, { status: 200, headers: out });
|
|
102
|
+
}
|