@angeloashmore/prismic-cli-poc 0.0.0-canary.1d36cd8

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.
Files changed (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +98 -0
  3. package/dist/index.mjs +2548 -0
  4. package/package.json +53 -0
  5. package/src/codegen-types.ts +82 -0
  6. package/src/codegen.ts +45 -0
  7. package/src/custom-type-add-field-boolean.ts +192 -0
  8. package/src/custom-type-add-field-color.ts +177 -0
  9. package/src/custom-type-add-field-date.ts +180 -0
  10. package/src/custom-type-add-field-embed.ts +177 -0
  11. package/src/custom-type-add-field-geo-point.ts +174 -0
  12. package/src/custom-type-add-field-image.ts +177 -0
  13. package/src/custom-type-add-field-key-text.ts +177 -0
  14. package/src/custom-type-add-field-link.ts +201 -0
  15. package/src/custom-type-add-field-number.ts +209 -0
  16. package/src/custom-type-add-field-rich-text.ts +202 -0
  17. package/src/custom-type-add-field-select.ts +192 -0
  18. package/src/custom-type-add-field-timestamp.ts +180 -0
  19. package/src/custom-type-add-field-uid.ts +177 -0
  20. package/src/custom-type-add-field.ts +111 -0
  21. package/src/custom-type-connect-slice.ts +220 -0
  22. package/src/custom-type-create.ts +118 -0
  23. package/src/custom-type-disconnect-slice.ts +177 -0
  24. package/src/custom-type-list.ts +110 -0
  25. package/src/custom-type-remove-field.ts +177 -0
  26. package/src/custom-type-remove.ts +144 -0
  27. package/src/custom-type-set-name.ts +144 -0
  28. package/src/custom-type-view.ts +118 -0
  29. package/src/custom-type.ts +85 -0
  30. package/src/index.ts +127 -0
  31. package/src/init.ts +64 -0
  32. package/src/lib/auth.ts +83 -0
  33. package/src/lib/config.ts +111 -0
  34. package/src/lib/custom-types-api.ts +438 -0
  35. package/src/lib/file.ts +49 -0
  36. package/src/lib/framework.ts +143 -0
  37. package/src/lib/json.ts +3 -0
  38. package/src/lib/request.ts +116 -0
  39. package/src/lib/slice.ts +115 -0
  40. package/src/lib/string.ts +6 -0
  41. package/src/lib/url.ts +25 -0
  42. package/src/locale-add.ts +116 -0
  43. package/src/locale-list.ts +107 -0
  44. package/src/locale-remove.ts +88 -0
  45. package/src/locale-set-default.ts +131 -0
  46. package/src/locale.ts +60 -0
  47. package/src/login.ts +152 -0
  48. package/src/logout.ts +36 -0
  49. package/src/page-type-add-field-boolean.ts +192 -0
  50. package/src/page-type-add-field-color.ts +177 -0
  51. package/src/page-type-add-field-date.ts +180 -0
  52. package/src/page-type-add-field-embed.ts +177 -0
  53. package/src/page-type-add-field-geo-point.ts +174 -0
  54. package/src/page-type-add-field-image.ts +177 -0
  55. package/src/page-type-add-field-key-text.ts +177 -0
  56. package/src/page-type-add-field-link.ts +201 -0
  57. package/src/page-type-add-field-number.ts +209 -0
  58. package/src/page-type-add-field-rich-text.ts +202 -0
  59. package/src/page-type-add-field-select.ts +192 -0
  60. package/src/page-type-add-field-timestamp.ts +180 -0
  61. package/src/page-type-add-field-uid.ts +177 -0
  62. package/src/page-type-add-field.ts +111 -0
  63. package/src/page-type-connect-slice.ts +220 -0
  64. package/src/page-type-create.ts +142 -0
  65. package/src/page-type-disconnect-slice.ts +177 -0
  66. package/src/page-type-list.ts +109 -0
  67. package/src/page-type-remove-field.ts +177 -0
  68. package/src/page-type-remove.ts +144 -0
  69. package/src/page-type-set-name.ts +144 -0
  70. package/src/page-type-set-repeatable.ts +153 -0
  71. package/src/page-type-view.ts +118 -0
  72. package/src/page-type.ts +90 -0
  73. package/src/preview-add.ts +126 -0
  74. package/src/preview-get-simulator.ts +104 -0
  75. package/src/preview-list.ts +106 -0
  76. package/src/preview-remove-simulator.ts +80 -0
  77. package/src/preview-remove.ts +109 -0
  78. package/src/preview-set-name.ts +137 -0
  79. package/src/preview-set-simulator.ts +116 -0
  80. package/src/preview.ts +75 -0
  81. package/src/pull.ts +247 -0
  82. package/src/push.ts +405 -0
  83. package/src/repo-create.ts +136 -0
  84. package/src/repo-get-access.ts +86 -0
  85. package/src/repo-list.ts +100 -0
  86. package/src/repo-set-access.ts +100 -0
  87. package/src/repo-set-name.ts +102 -0
  88. package/src/repo-view.ts +113 -0
  89. package/src/repo.ts +70 -0
  90. package/src/slice-add-field-boolean.ts +173 -0
  91. package/src/slice-add-field-color.ts +158 -0
  92. package/src/slice-add-field-date.ts +158 -0
  93. package/src/slice-add-field-embed.ts +158 -0
  94. package/src/slice-add-field-geo-point.ts +155 -0
  95. package/src/slice-add-field-image.ts +155 -0
  96. package/src/slice-add-field-key-text.ts +158 -0
  97. package/src/slice-add-field-link.ts +178 -0
  98. package/src/slice-add-field-number.ts +158 -0
  99. package/src/slice-add-field-rich-text.ts +183 -0
  100. package/src/slice-add-field-select.ts +173 -0
  101. package/src/slice-add-field-timestamp.ts +158 -0
  102. package/src/slice-add-field.ts +106 -0
  103. package/src/slice-add-variation.ts +145 -0
  104. package/src/slice-create.ts +148 -0
  105. package/src/slice-list-variations.ts +67 -0
  106. package/src/slice-list.ts +88 -0
  107. package/src/slice-remove-field.ts +128 -0
  108. package/src/slice-remove-variation.ts +118 -0
  109. package/src/slice-remove.ts +97 -0
  110. package/src/slice-rename.ts +128 -0
  111. package/src/slice-view.ts +77 -0
  112. package/src/slice.ts +90 -0
  113. package/src/status.ts +733 -0
  114. package/src/token-create.ts +203 -0
  115. package/src/token-delete.ts +182 -0
  116. package/src/token-list.ts +223 -0
  117. package/src/token-set-name.ts +193 -0
  118. package/src/token.ts +60 -0
  119. package/src/webhook-add-header.ts +118 -0
  120. package/src/webhook-create.ts +152 -0
  121. package/src/webhook-disable.ts +109 -0
  122. package/src/webhook-enable.ts +132 -0
  123. package/src/webhook-list.ts +93 -0
  124. package/src/webhook-remove-header.ts +117 -0
  125. package/src/webhook-remove.ts +106 -0
  126. package/src/webhook-set-triggers.ts +148 -0
  127. package/src/webhook-status.ts +90 -0
  128. package/src/webhook-test.ts +106 -0
  129. package/src/webhook-view.ts +147 -0
  130. package/src/webhook.ts +95 -0
  131. package/src/whoami.ts +62 -0
@@ -0,0 +1,203 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { safeGetRepositoryFromConfig } from "./lib/config";
6
+ import { stringify } from "./lib/json";
7
+ import { ForbiddenRequestError, request, UnauthorizedRequestError } from "./lib/request";
8
+ import { getRepoUrl } from "./lib/url";
9
+ import {
10
+ type AccessToken,
11
+ AccessTokenSchema,
12
+ getAccessTokens,
13
+ type OAuthApp,
14
+ OAuthAppSchema,
15
+ type WriteToken,
16
+ WriteTokenSchema,
17
+ } from "./token-list";
18
+
19
+ const HELP = `
20
+ Create a new API token for a Prismic repository.
21
+
22
+ By default, this command reads the repository from prismic.config.json at the
23
+ project root.
24
+
25
+ USAGE
26
+ prismic token create [flags]
27
+
28
+ FLAGS
29
+ -w, --write Create a write token (Custom Types/Migration API)
30
+ -n, --name string Token name (default: "Prismic CLI")
31
+ --allow-releases Allow access to releases (access tokens only)
32
+ --json Output as JSON
33
+ -r, --repo string Repository domain
34
+ -h, --help Show help for command
35
+
36
+ LEARN MORE
37
+ Use \`prismic token <command> --help\` for more information about a command.
38
+ `.trim();
39
+
40
+ const DEFAULT_APP_NAME = "Prismic CLI";
41
+
42
+ export async function tokenCreate(): Promise<void> {
43
+ const {
44
+ values: {
45
+ help,
46
+ repo = await safeGetRepositoryFromConfig(),
47
+ json,
48
+ write,
49
+ name = DEFAULT_APP_NAME,
50
+ "allow-releases": allowReleases,
51
+ },
52
+ } = parseArgs({
53
+ args: process.argv.slice(4), // skip: node, script, "token", "create"
54
+ options: {
55
+ json: { type: "boolean" },
56
+ repo: { type: "string", short: "r" },
57
+ help: { type: "boolean", short: "h" },
58
+ write: { type: "boolean", short: "w" },
59
+ name: { type: "string", short: "n" },
60
+ "allow-releases": { type: "boolean" },
61
+ },
62
+ allowPositionals: false,
63
+ });
64
+
65
+ if (help) {
66
+ console.info(HELP);
67
+ return;
68
+ }
69
+
70
+ if (!repo) {
71
+ console.error("Missing prismic.config.json or --repo option");
72
+ process.exitCode = 1;
73
+ return;
74
+ }
75
+
76
+ if (write && allowReleases) {
77
+ console.error("--allow-releases is only valid for access tokens (not with --write)");
78
+ process.exitCode = 1;
79
+ return;
80
+ }
81
+
82
+ const authenticated = await isAuthenticated();
83
+ if (!authenticated) {
84
+ handleUnauthenticated();
85
+ return;
86
+ }
87
+
88
+ if (write) {
89
+ const result = await createWriteToken(repo, name);
90
+ if (!result.ok) {
91
+ if (
92
+ result.error instanceof ForbiddenRequestError ||
93
+ result.error instanceof UnauthorizedRequestError
94
+ ) {
95
+ handleUnauthenticated();
96
+ } else if (v.isValiError(result.error)) {
97
+ console.error(
98
+ `Failed to create write token: Invalid response: ${stringify(result.error.issues)}`,
99
+ );
100
+ process.exitCode = 1;
101
+ } else {
102
+ console.error(`Failed to create write token: ${stringify(result.value)}`);
103
+ process.exitCode = 1;
104
+ }
105
+ return;
106
+ }
107
+
108
+ if (json) {
109
+ console.info(stringify(result.value));
110
+ } else {
111
+ console.info(`Token created: ${result.value.token}`);
112
+ }
113
+ } else {
114
+ const scope = allowReleases ? "master+releases" : "master";
115
+ const result = await createAccessToken(repo, name, scope);
116
+ if (!result.ok) {
117
+ if (
118
+ result.error instanceof ForbiddenRequestError ||
119
+ result.error instanceof UnauthorizedRequestError
120
+ ) {
121
+ handleUnauthenticated();
122
+ } else if (v.isValiError(result.error)) {
123
+ console.error(
124
+ `Failed to create access token: Invalid response: ${stringify(result.error.issues)}`,
125
+ );
126
+ process.exitCode = 1;
127
+ } else {
128
+ console.error(`Failed to create access token: ${stringify(result.value)}`);
129
+ process.exitCode = 1;
130
+ }
131
+ return;
132
+ }
133
+
134
+ if (json) {
135
+ console.info(stringify(result.value));
136
+ } else {
137
+ console.info(`Token created: ${result.value.token}`);
138
+ }
139
+ }
140
+ }
141
+
142
+ type CreateWriteTokenResult =
143
+ | { ok: true; value: WriteToken }
144
+ | { ok: false; value: unknown; error: Error | v.ValiError<typeof WriteTokenSchema> };
145
+
146
+ async function createWriteToken(repo: string, appName: string): Promise<CreateWriteTokenResult> {
147
+ const url = new URL("settings/security/token", await getRepoUrl(repo));
148
+ const response = await request(url, {
149
+ method: "POST",
150
+ body: { app_name: appName },
151
+ schema: WriteTokenSchema,
152
+ });
153
+ return response;
154
+ }
155
+
156
+ type CreateAccessTokenResult =
157
+ | { ok: true; value: AccessToken }
158
+ | { ok: false; value: unknown; error: Error | v.ValiError<typeof AccessTokenSchema> };
159
+
160
+ async function createAccessToken(
161
+ repo: string,
162
+ appName: string,
163
+ scope: "master" | "master+releases",
164
+ ): Promise<CreateAccessTokenResult> {
165
+ // First, find or create an OAuth app with the given name
166
+ const appsResponse = await getAccessTokens(repo);
167
+ if (!appsResponse.ok) {
168
+ return appsResponse;
169
+ }
170
+
171
+ let app = appsResponse.value.find((a: OAuthApp) => a.name === appName);
172
+
173
+ // Create OAuth app if it doesn't exist
174
+ if (!app) {
175
+ const createAppUrl = new URL("settings/security/oauthapp", await getRepoUrl(repo));
176
+ const createAppResponse = await request(createAppUrl, {
177
+ method: "POST",
178
+ body: { app_name: appName },
179
+ schema: OAuthAppSchema,
180
+ });
181
+
182
+ if (!createAppResponse.ok) {
183
+ return createAppResponse;
184
+ }
185
+
186
+ app = createAppResponse.value;
187
+ }
188
+
189
+ // Create the authorization token
190
+ const authUrl = new URL("settings/security/authorizations", await getRepoUrl(repo));
191
+ const authResponse = await request(authUrl, {
192
+ method: "POST",
193
+ body: { app: app.id, scope },
194
+ schema: AccessTokenSchema,
195
+ });
196
+
197
+ return authResponse;
198
+ }
199
+
200
+ function handleUnauthenticated(): void {
201
+ console.error("Not logged in. Run `prismic login` first.");
202
+ process.exitCode = 1;
203
+ }
@@ -0,0 +1,182 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { safeGetRepositoryFromConfig } from "./lib/config";
6
+ import { stringify } from "./lib/json";
7
+ import { ForbiddenRequestError, request, UnauthorizedRequestError } from "./lib/request";
8
+ import { getRepoUrl } from "./lib/url";
9
+ import { type AccessToken, getAccessTokens, getWriteTokens, type WriteToken } from "./token-list";
10
+
11
+ const HELP = `
12
+ Delete a token from a Prismic repository.
13
+
14
+ By default, this command reads the repository from prismic.config.json at the
15
+ project root.
16
+
17
+ USAGE
18
+ prismic token delete <token> [flags]
19
+
20
+ ARGUMENTS
21
+ token The token value (or partial match)
22
+
23
+ FLAGS
24
+ -r, --repo string Repository domain
25
+ -h, --help Show help for command
26
+
27
+ LEARN MORE
28
+ Use \`prismic token <command> --help\` for more information about a command.
29
+ `.trim();
30
+
31
+ export async function tokenDelete(): Promise<void> {
32
+ const {
33
+ values: { help, repo = await safeGetRepositoryFromConfig() },
34
+ positionals: [tokenValue],
35
+ } = parseArgs({
36
+ args: process.argv.slice(4), // skip: node, script, "token", "delete"
37
+ options: {
38
+ repo: { type: "string", short: "r" },
39
+ help: { type: "boolean", short: "h" },
40
+ },
41
+ allowPositionals: true,
42
+ });
43
+
44
+ if (help) {
45
+ console.info(HELP);
46
+ return;
47
+ }
48
+
49
+ if (!tokenValue) {
50
+ console.error("Missing required argument: token");
51
+ process.exitCode = 1;
52
+ return;
53
+ }
54
+
55
+ if (!repo) {
56
+ console.error("Missing prismic.config.json or --repo option");
57
+ process.exitCode = 1;
58
+ return;
59
+ }
60
+
61
+ const authenticated = await isAuthenticated();
62
+ if (!authenticated) {
63
+ handleUnauthenticated();
64
+ return;
65
+ }
66
+
67
+ // First, find the token in access tokens or write tokens
68
+ const [accessResponse, writeResponse] = await Promise.all([
69
+ getAccessTokens(repo),
70
+ getWriteTokens(repo),
71
+ ]);
72
+
73
+ if (!accessResponse.ok) {
74
+ if (
75
+ accessResponse.error instanceof ForbiddenRequestError ||
76
+ accessResponse.error instanceof UnauthorizedRequestError
77
+ ) {
78
+ handleUnauthenticated();
79
+ } else if (v.isValiError(accessResponse.error)) {
80
+ console.error(
81
+ `Failed to list access tokens: Invalid response: ${stringify(accessResponse.error.issues)}`,
82
+ );
83
+ process.exitCode = 1;
84
+ } else {
85
+ console.error(`Failed to list access tokens: ${stringify(accessResponse.value)}`);
86
+ process.exitCode = 1;
87
+ }
88
+ return;
89
+ }
90
+
91
+ if (!writeResponse.ok) {
92
+ if (
93
+ writeResponse.error instanceof ForbiddenRequestError ||
94
+ writeResponse.error instanceof UnauthorizedRequestError
95
+ ) {
96
+ handleUnauthenticated();
97
+ } else if (v.isValiError(writeResponse.error)) {
98
+ console.error(
99
+ `Failed to list write tokens: Invalid response: ${stringify(writeResponse.error.issues)}`,
100
+ );
101
+ process.exitCode = 1;
102
+ } else {
103
+ console.error(`Failed to list write tokens: ${stringify(writeResponse.value)}`);
104
+ process.exitCode = 1;
105
+ }
106
+ return;
107
+ }
108
+
109
+ // Find in access tokens
110
+ let foundAuth: AccessToken | undefined;
111
+ for (const app of accessResponse.value) {
112
+ for (const auth of app.wroom_auths) {
113
+ if (
114
+ auth.token === tokenValue ||
115
+ auth.token.startsWith(tokenValue) ||
116
+ auth.token.endsWith(tokenValue)
117
+ ) {
118
+ foundAuth = auth;
119
+ break;
120
+ }
121
+ }
122
+ if (foundAuth) break;
123
+ }
124
+
125
+ if (foundAuth) {
126
+ // Delete the authorization (preserves OAuth app)
127
+ const url = new URL(`settings/security/authorizations/${foundAuth.id}`, await getRepoUrl(repo));
128
+ const response = await request(url, { method: "DELETE" });
129
+
130
+ if (!response.ok) {
131
+ if (
132
+ response.error instanceof ForbiddenRequestError ||
133
+ response.error instanceof UnauthorizedRequestError
134
+ ) {
135
+ handleUnauthenticated();
136
+ } else {
137
+ console.error(`Failed to delete token: ${stringify(response.value)}`);
138
+ process.exitCode = 1;
139
+ }
140
+ return;
141
+ }
142
+
143
+ console.info("Token deleted");
144
+ return;
145
+ }
146
+
147
+ // Find in write tokens
148
+ const foundWriteToken = writeResponse.value.tokens.find(
149
+ (t: WriteToken) =>
150
+ t.token === tokenValue || t.token.startsWith(tokenValue) || t.token.endsWith(tokenValue),
151
+ );
152
+
153
+ if (foundWriteToken) {
154
+ // Delete write token
155
+ const url = new URL(`settings/security/token/${foundWriteToken.token}`, await getRepoUrl(repo));
156
+ const response = await request(url, { method: "DELETE" });
157
+
158
+ if (!response.ok) {
159
+ if (
160
+ response.error instanceof ForbiddenRequestError ||
161
+ response.error instanceof UnauthorizedRequestError
162
+ ) {
163
+ handleUnauthenticated();
164
+ } else {
165
+ console.error(`Failed to delete token: ${stringify(response.value)}`);
166
+ process.exitCode = 1;
167
+ }
168
+ return;
169
+ }
170
+
171
+ console.info("Token deleted");
172
+ return;
173
+ }
174
+
175
+ console.error(`Token not found: ${tokenValue}`);
176
+ process.exitCode = 1;
177
+ }
178
+
179
+ function handleUnauthenticated(): void {
180
+ console.error("Not logged in. Run `prismic login` first.");
181
+ process.exitCode = 1;
182
+ }
@@ -0,0 +1,223 @@
1
+ import { parseArgs } from "node:util";
2
+ import * as v from "valibot";
3
+
4
+ import { isAuthenticated } from "./lib/auth";
5
+ import { safeGetRepositoryFromConfig } from "./lib/config";
6
+ import { stringify } from "./lib/json";
7
+ import {
8
+ ForbiddenRequestError,
9
+ type ParsedRequestResponse,
10
+ request,
11
+ UnauthorizedRequestError,
12
+ } from "./lib/request";
13
+ import { getRepoUrl } from "./lib/url";
14
+
15
+ const HELP = `
16
+ List all API tokens for a Prismic repository.
17
+
18
+ By default, this command reads the repository from prismic.config.json at the
19
+ project root.
20
+
21
+ USAGE
22
+ prismic token list [flags]
23
+
24
+ FLAGS
25
+ --json Output as JSON
26
+ -r, --repo string Repository domain
27
+ -h, --help Show help for command
28
+
29
+ LEARN MORE
30
+ Use \`prismic token <command> --help\` for more information about a command.
31
+ `.trim();
32
+
33
+ export async function tokenList(): Promise<void> {
34
+ const {
35
+ values: { help, repo = await safeGetRepositoryFromConfig(), json },
36
+ } = parseArgs({
37
+ args: process.argv.slice(4), // skip: node, script, "token", "list"
38
+ options: {
39
+ json: { type: "boolean" },
40
+ repo: { type: "string", short: "r" },
41
+ help: { type: "boolean", short: "h" },
42
+ },
43
+ allowPositionals: false,
44
+ });
45
+
46
+ if (help) {
47
+ console.info(HELP);
48
+ return;
49
+ }
50
+
51
+ if (!repo) {
52
+ console.error("Missing prismic.config.json or --repo option");
53
+ process.exitCode = 1;
54
+ return;
55
+ }
56
+
57
+ const authenticated = await isAuthenticated();
58
+ if (!authenticated) {
59
+ handleUnauthenticated();
60
+ return;
61
+ }
62
+
63
+ const [accessResponse, writeResponse] = await Promise.all([
64
+ getAccessTokens(repo),
65
+ getWriteTokens(repo),
66
+ ]);
67
+
68
+ if (!accessResponse.ok) {
69
+ if (
70
+ accessResponse.error instanceof ForbiddenRequestError ||
71
+ accessResponse.error instanceof UnauthorizedRequestError
72
+ ) {
73
+ handleUnauthenticated();
74
+ } else if (v.isValiError(accessResponse.error)) {
75
+ console.error(
76
+ `Failed to list access tokens: Invalid response: ${stringify(accessResponse.error.issues)}`,
77
+ );
78
+ process.exitCode = 1;
79
+ } else {
80
+ console.error(`Failed to list access tokens: ${stringify(accessResponse.value)}`);
81
+ process.exitCode = 1;
82
+ }
83
+ return;
84
+ }
85
+
86
+ if (!writeResponse.ok) {
87
+ if (
88
+ writeResponse.error instanceof ForbiddenRequestError ||
89
+ writeResponse.error instanceof UnauthorizedRequestError
90
+ ) {
91
+ handleUnauthenticated();
92
+ } else if (v.isValiError(writeResponse.error)) {
93
+ console.error(
94
+ `Failed to list write tokens: Invalid response: ${stringify(writeResponse.error.issues)}`,
95
+ );
96
+ process.exitCode = 1;
97
+ } else {
98
+ console.error(`Failed to list write tokens: ${stringify(writeResponse.value)}`);
99
+ process.exitCode = 1;
100
+ }
101
+ return;
102
+ }
103
+
104
+ const accessTokens = accessResponse.value.flatMap((app: OAuthApp) =>
105
+ app.wroom_auths.map((auth: AccessToken) => ({
106
+ name: app.name,
107
+ appId: app.id,
108
+ authId: auth.id,
109
+ scope: auth.scope,
110
+ token: auth.token,
111
+ createdAt: auth.created_at.$date,
112
+ })),
113
+ );
114
+ const writeTokens = writeResponse.value.tokens;
115
+
116
+ if (json) {
117
+ console.info(stringify({ accessTokens, writeTokens }));
118
+ } else {
119
+ if (accessTokens.length > 0) {
120
+ console.info("ACCESS TOKENS");
121
+ for (const token of accessTokens) {
122
+ const truncated = truncateToken(token.token);
123
+ const date = formatDate(token.createdAt);
124
+ console.info(` ${token.name} ${token.scope} ${truncated} ${date}`);
125
+ }
126
+ } else {
127
+ console.info("ACCESS TOKENS (none)");
128
+ }
129
+
130
+ console.info("");
131
+
132
+ if (writeTokens.length > 0) {
133
+ console.info("WRITE TOKENS");
134
+ for (const token of writeTokens) {
135
+ const truncated = truncateToken(token.token);
136
+ const date = formatDate(token.timestamp);
137
+ console.info(` ${token.app_name} ${truncated} ${date}`);
138
+ }
139
+ } else {
140
+ console.info("WRITE TOKENS (none)");
141
+ }
142
+ }
143
+ }
144
+
145
+ // MongoDB date format: { "$date": milliseconds }
146
+ const MongoDBDateSchema = v.object({ $date: v.number() });
147
+ type MongoDBDate = v.InferOutput<typeof MongoDBDateSchema>;
148
+
149
+ // Access Token (from OAuth app's wroom_auths array)
150
+ export const AccessTokenSchema = v.object({
151
+ id: v.string(),
152
+ origin: v.string(),
153
+ domain: v.string(),
154
+ app: v.string(),
155
+ scope: v.string(),
156
+ expired_at: MongoDBDateSchema,
157
+ created_at: MongoDBDateSchema,
158
+ owner: v.nullable(v.string()),
159
+ token: v.string(),
160
+ });
161
+ export type AccessToken = v.InferOutput<typeof AccessTokenSchema>;
162
+
163
+ // OAuth App (container for access tokens)
164
+ export const OAuthAppSchema = v.object({
165
+ id: v.string(),
166
+ secret: v.string(),
167
+ name: v.string(),
168
+ owner: v.string(),
169
+ created_at: MongoDBDateSchema,
170
+ authorized_domains: v.array(v.string()),
171
+ wroom_auths: v.array(AccessTokenSchema),
172
+ });
173
+ export type OAuthApp = v.InferOutput<typeof OAuthAppSchema>;
174
+
175
+ // Write Token
176
+ export const WriteTokenSchema = v.object({
177
+ app_name: v.string(),
178
+ token: v.string(),
179
+ timestamp: v.number(),
180
+ });
181
+ export type WriteToken = v.InferOutput<typeof WriteTokenSchema>;
182
+
183
+ // Write Tokens List Response
184
+ const WriteTokensInfoSchema = v.object({
185
+ max_tokens: v.number(),
186
+ tokens: v.array(WriteTokenSchema),
187
+ });
188
+ type WriteTokensInfo = v.InferOutput<typeof WriteTokensInfoSchema>;
189
+
190
+ // Response schemas
191
+ const GetAccessTokensResponseSchema = v.array(OAuthAppSchema);
192
+ type GetAccessTokensResponse = v.InferOutput<typeof GetAccessTokensResponseSchema>;
193
+
194
+ export async function getAccessTokens(
195
+ repo: string,
196
+ ): Promise<ParsedRequestResponse<GetAccessTokensResponse>> {
197
+ const url = new URL("settings/security/contentapi", await getRepoUrl(repo));
198
+ return await request(url, { schema: GetAccessTokensResponseSchema });
199
+ }
200
+
201
+ export async function getWriteTokens(
202
+ repo: string,
203
+ ): Promise<ParsedRequestResponse<WriteTokensInfo>> {
204
+ const url = new URL("settings/security/customtypesapi", await getRepoUrl(repo));
205
+ return await request(url, { schema: WriteTokensInfoSchema });
206
+ }
207
+
208
+ function truncateToken(token: string): string {
209
+ if (token.length <= 12) return token;
210
+ return `${token.slice(0, 8)}...${token.slice(-4)}`;
211
+ }
212
+
213
+ function formatDate(timestamp: number | MongoDBDate): string {
214
+ // MongoDB dates are in milliseconds, plain numbers are in seconds
215
+ const ms = typeof timestamp === "number" ? timestamp * 1000 : timestamp.$date;
216
+ const date = new Date(ms);
217
+ return date.toISOString().split("T")[0];
218
+ }
219
+
220
+ function handleUnauthenticated(): void {
221
+ console.error("Not logged in. Run `prismic login` first.");
222
+ process.exitCode = 1;
223
+ }