Skip to content

Commit 832057b

Browse files
authored
Merge pull request #4288 from cardstack/worktree-cs-10550-decorator-transforms
Replace hardcoded MIME types with SupportedMimeType from runtime-common (CS-10550)
2 parents 8831178 + d3340c2 commit 832057b

28 files changed

Lines changed: 223 additions & 181 deletions

packages/runtime-common/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export const isNode =
309309
Object.prototype.toString.call((globalThis as any).process) ===
310310
'[object process]';
311311

312-
export { SupportedMimeType } from './router';
312+
export { SupportedMimeType } from './supported-mime-type';
313313
export {
314314
isUrlLike,
315315
VirtualNetwork,

packages/runtime-common/router.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,8 @@ function formatUnknownError(error: unknown): string {
4747

4848
export type Method = 'GET' | 'QUERY' | 'POST' | 'PATCH' | 'DELETE' | 'HEAD';
4949

50-
/* eslint-disable @typescript-eslint/no-duplicate-enum-values */
51-
export enum SupportedMimeType {
52-
CardJson = 'application/vnd.card+json',
53-
CardSource = 'application/vnd.card+source',
54-
FileMeta = 'application/vnd.card.file-meta+json',
55-
DirectoryListing = 'application/vnd.api+json',
56-
RealmInfo = 'application/vnd.api+json',
57-
Mtimes = 'application/vnd.api+json',
58-
Permissions = 'application/vnd.api+json',
59-
Session = 'application/json',
60-
EventStream = 'text/event-stream',
61-
HTML = 'text/html',
62-
JSONAPI = 'application/vnd.api+json',
63-
JSON = 'application/json',
64-
CardDependencies = 'application/json',
65-
CardTypeSummary = 'application/json',
66-
OctetStream = 'application/octet-stream',
67-
All = '*/*',
68-
}
69-
/* eslint-enable @typescript-eslint/no-duplicate-enum-values */
50+
import { SupportedMimeType } from './supported-mime-type';
51+
export { SupportedMimeType };
7052

7153
function isHTTPMethod(method: unknown): method is Method {
7254
if (typeof method !== 'string') {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable @typescript-eslint/no-duplicate-enum-values */
2+
export enum SupportedMimeType {
3+
CardJson = 'application/vnd.card+json',
4+
CardSource = 'application/vnd.card+source',
5+
FileMeta = 'application/vnd.card.file-meta+json',
6+
DirectoryListing = 'application/vnd.api+json',
7+
RealmInfo = 'application/vnd.api+json',
8+
Mtimes = 'application/vnd.api+json',
9+
Permissions = 'application/vnd.api+json',
10+
Session = 'application/json',
11+
EventStream = 'text/event-stream',
12+
HTML = 'text/html',
13+
JSONAPI = 'application/vnd.api+json',
14+
JSON = 'application/json',
15+
CardDependencies = 'application/json',
16+
CardTypeSummary = 'application/json',
17+
OctetStream = 'application/octet-stream',
18+
All = '*/*',
19+
}
20+
/* eslint-enable @typescript-eslint/no-duplicate-enum-values */

packages/software-factory/scripts/factory-tools-smoke.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* pnpm factory:tools-smoke
99
*/
1010

11+
import { SupportedMimeType } from '@cardstack/runtime-common/supported-mime-type';
12+
1113
import type { AgentAction } from './lib/factory-agent';
1214
import {
1315
ToolExecutor,
@@ -188,7 +190,7 @@ async function main(): Promise<void> {
188190
JSON.stringify({
189191
data: [{ id: 'CardDef/hello', type: 'card' }],
190192
}),
191-
{ status: 200, headers: { 'Content-Type': 'application/json' } },
193+
{ status: 200, headers: { 'Content-Type': SupportedMimeType.JSON } },
192194
);
193195
}) as typeof globalThis.fetch,
194196
});

packages/software-factory/scripts/lib/boxel.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { homedir } from 'node:os';
33
import { join } from 'node:path';
44

55
import { formatErrorResponse } from '../../src/error-format';
6-
import { ensureTrailingSlash } from './realm-operations';
6+
import { ensureTrailingSlash, SupportedMimeType } from './realm-operations';
77

88
const PROFILES_FILE = join(homedir(), '.boxel-cli', 'profiles.json');
99

@@ -133,7 +133,7 @@ export async function matrixLogin(
133133
{
134134
method: 'POST',
135135
headers: {
136-
'Content-Type': 'application/json',
136+
'Content-Type': SupportedMimeType.JSON,
137137
},
138138
body: JSON.stringify({
139139
identifier: {
@@ -173,7 +173,7 @@ export async function getOpenIdToken(
173173
{
174174
method: 'POST',
175175
headers: {
176-
'Content-Type': 'application/json',
176+
'Content-Type': SupportedMimeType.JSON,
177177
Authorization: `Bearer ${matrixAuth.accessToken}`,
178178
},
179179
body: '{}',
@@ -197,8 +197,8 @@ export async function getRealmServerToken(
197197
{
198198
method: 'POST',
199199
headers: {
200-
Accept: 'application/json',
201-
'Content-Type': 'application/json',
200+
Accept: SupportedMimeType.JSON,
201+
'Content-Type': SupportedMimeType.JSON,
202202
},
203203
body: JSON.stringify(openIdToken),
204204
},
@@ -229,8 +229,8 @@ export async function getAccessibleRealmTokens(
229229
{
230230
method: 'POST',
231231
headers: {
232-
Accept: 'application/json',
233-
'Content-Type': 'application/json',
232+
Accept: SupportedMimeType.JSON,
233+
'Content-Type': SupportedMimeType.JSON,
234234
Authorization: serverToken,
235235
},
236236
},
@@ -281,8 +281,8 @@ export async function searchRealm(input: {
281281
{
282282
method: 'QUERY',
283283
headers: {
284-
Accept: 'application/vnd.card+json',
285-
'Content-Type': 'application/json',
284+
Accept: SupportedMimeType.CardJson,
285+
'Content-Type': SupportedMimeType.JSON,
286286
...(input.jwt ? { Authorization: input.jwt } : {}),
287287
},
288288
body: JSON.stringify(input.query),

packages/software-factory/scripts/lib/factory-agent.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { SupportedMimeType } from '@cardstack/runtime-common/supported-mime-type';
2+
13
import { createBoxelRealmFetch } from '../../src/realm-auth';
24

35
import {
@@ -486,8 +488,8 @@ export class OpenRouterFactoryAgent implements FactoryAgent {
486488
response = await this.fetchImpl(OPENROUTER_CHAT_URL, {
487489
method: 'POST',
488490
headers: {
489-
Accept: 'application/json',
490-
'Content-Type': 'application/json',
491+
Accept: SupportedMimeType.JSON,
492+
'Content-Type': SupportedMimeType.JSON,
491493
},
492494
body: JSON.stringify({
493495
model: this.config.model,
@@ -505,8 +507,8 @@ export class OpenRouterFactoryAgent implements FactoryAgent {
505507
response = await this.fetchImpl(proxyUrl, {
506508
method: 'POST',
507509
headers: {
508-
Accept: 'application/json',
509-
'Content-Type': 'application/json',
510+
Accept: SupportedMimeType.JSON,
511+
'Content-Type': SupportedMimeType.JSON,
510512
},
511513
body: JSON.stringify({
512514
url: OPENROUTER_CHAT_URL,

packages/software-factory/scripts/lib/factory-tool-executor.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { spawn } from 'node:child_process';
22
import { resolve } from 'node:path';
33

4+
import { SupportedMimeType } from '@cardstack/runtime-common/supported-mime-type';
45
import {
56
iconURLFor,
67
getRandomBackgroundURL,
@@ -585,7 +586,7 @@ export class ToolExecutor {
585586
let accountDataUrl = `${baseUrl}_matrix/client/v3/user/${encodedUserId}/account_data/app.boxel.realms`;
586587
let authHeaders = {
587588
Authorization: `Bearer ${matrixAccessToken}`,
588-
'Content-Type': 'application/json',
589+
'Content-Type': SupportedMimeType.JSON,
589590
};
590591

591592
// GET current realm list
@@ -761,7 +762,7 @@ function buildRealmApiRequest(
761762
authorization?: string,
762763
): RealmApiRequestParams {
763764
let headers: Record<string, string> = {
764-
Accept: 'application/json',
765+
Accept: SupportedMimeType.JSON,
765766
};
766767

767768
if (authorization) {
@@ -775,7 +776,7 @@ function buildRealmApiRequest(
775776
let accept =
776777
typeof toolArgs['accept'] === 'string'
777778
? toolArgs['accept']
778-
: 'application/vnd.card+source';
779+
: SupportedMimeType.CardSource;
779780
return {
780781
url: `${realmUrl}${path}`,
781782
method: 'GET',
@@ -790,7 +791,7 @@ function buildRealmApiRequest(
790791
let contentType =
791792
typeof toolArgs['content-type'] === 'string'
792793
? toolArgs['content-type']
793-
: 'application/vnd.card+source';
794+
: SupportedMimeType.CardSource;
794795
return {
795796
url: `${realmUrl}${path}`,
796797
method: 'POST',
@@ -817,7 +818,7 @@ function buildRealmApiRequest(
817818
method: 'POST',
818819
headers: {
819820
...headers,
820-
'Content-Type': 'application/vnd.api+json',
821+
'Content-Type': SupportedMimeType.JSONAPI,
821822
},
822823
body: JSON.stringify({ 'atomic:operations': JSON.parse(operations) }),
823824
};
@@ -831,8 +832,8 @@ function buildRealmApiRequest(
831832
method: 'QUERY',
832833
headers: {
833834
...headers,
834-
Accept: 'application/vnd.card+json',
835-
'Content-Type': 'application/json',
835+
Accept: SupportedMimeType.CardJson,
836+
'Content-Type': SupportedMimeType.JSON,
836837
},
837838
body: query,
838839
};
@@ -855,8 +856,8 @@ function buildRealmApiRequest(
855856
method: 'POST',
856857
headers: {
857858
...headers,
858-
Accept: 'application/vnd.api+json',
859-
'Content-Type': 'application/vnd.api+json',
859+
Accept: SupportedMimeType.JSONAPI,
860+
'Content-Type': SupportedMimeType.JSONAPI,
860861
},
861862
body: JSON.stringify({
862863
data: {
@@ -878,7 +879,7 @@ function buildRealmApiRequest(
878879
return {
879880
url: `${serverUrl}_server-session`,
880881
method: 'POST',
881-
headers: { ...headers, 'Content-Type': 'application/json' },
882+
headers: { ...headers, 'Content-Type': SupportedMimeType.JSON },
882883
body: JSON.stringify({ access_token: openidToken }),
883884
};
884885
}
@@ -888,7 +889,7 @@ function buildRealmApiRequest(
888889
return {
889890
url: `${serverUrl}_realm-auth`,
890891
method: 'POST',
891-
headers: { ...headers, 'Content-Type': 'application/json' },
892+
headers: { ...headers, 'Content-Type': SupportedMimeType.JSON },
892893
};
893894
}
894895

packages/software-factory/scripts/lib/realm-operations.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import {
1515
getRandomBackgroundURL,
1616
} from '@cardstack/runtime-common/realm-display-defaults';
1717

18-
// ---------------------------------------------------------------------------
19-
// Constants
20-
// ---------------------------------------------------------------------------
18+
import { SupportedMimeType } from '@cardstack/runtime-common/supported-mime-type';
19+
20+
export { SupportedMimeType };
2121

22-
export const cardSourceMimeType = 'application/vnd.card+source';
22+
export function ensureTrailingSlash(url: string): string {
23+
return url.endsWith('/') ? url : `${url}/`;
24+
}
2325

2426
// ---------------------------------------------------------------------------
2527
// Types
@@ -38,15 +40,9 @@ export interface SearchRealmOptions extends RealmFetchOptions {
3840
// Helpers
3941
// ---------------------------------------------------------------------------
4042

41-
// Note: also exists in @cardstack/runtime-common/paths but not exported
42-
// from the package index. Kept here to avoid subpath import issues.
43-
export function ensureTrailingSlash(url: string): string {
44-
return url.endsWith('/') ? url : `${url}/`;
45-
}
46-
4743
export function buildAuthHeaders(
4844
authorization?: string,
49-
accept = 'application/json',
45+
accept = SupportedMimeType.JSON,
5046
): Record<string, string> {
5147
let headers: Record<string, string> = { Accept: accept };
5248
if (authorization) {
@@ -59,8 +55,8 @@ export function buildCardSourceHeaders(
5955
authorization?: string,
6056
): Record<string, string> {
6157
let headers: Record<string, string> = {
62-
Accept: cardSourceMimeType,
63-
'Content-Type': cardSourceMimeType,
58+
Accept: SupportedMimeType.CardSource,
59+
'Content-Type': SupportedMimeType.CardSource,
6460
};
6561
if (authorization) {
6662
headers['Authorization'] = authorization;
@@ -86,8 +82,8 @@ export async function searchRealm(
8682
let searchUrl = `${normalizedUrl}_search`;
8783

8884
let headers: Record<string, string> = {
89-
Accept: 'application/vnd.card+json',
90-
'Content-Type': 'application/json',
85+
Accept: SupportedMimeType.CardJson,
86+
'Content-Type': SupportedMimeType.JSON,
9187
};
9288
if (options?.authorization) {
9389
headers['Authorization'] = options.authorization;
@@ -132,7 +128,10 @@ export async function readCardSource(
132128
try {
133129
let response = await fetchImpl(url, {
134130
method: 'GET',
135-
headers: buildAuthHeaders(options?.authorization, cardSourceMimeType),
131+
headers: buildAuthHeaders(
132+
options?.authorization,
133+
SupportedMimeType.CardSource,
134+
),
136135
});
137136

138137
if (!response.ok) {
@@ -244,8 +243,8 @@ export async function cancelAllIndexingJobs(
244243
let cancelUrl = `${normalizedUrl}_cancel-indexing-job`;
245244

246245
let headers: Record<string, string> = {
247-
Accept: 'application/json',
248-
'Content-Type': 'application/json',
246+
Accept: SupportedMimeType.JSON,
247+
'Content-Type': SupportedMimeType.JSON,
249248
};
250249
if (options?.authorization) {
251250
headers['Authorization'] = options.authorization;
@@ -307,8 +306,8 @@ export async function createRealm(
307306
let normalizedUrl = ensureTrailingSlash(realmServerUrl);
308307

309308
let headers: Record<string, string> = {
310-
Accept: 'application/vnd.api+json',
311-
'Content-Type': 'application/vnd.api+json',
309+
Accept: SupportedMimeType.JSONAPI,
310+
'Content-Type': SupportedMimeType.JSONAPI,
312311
Authorization: options.authorization,
313312
};
314313

@@ -386,8 +385,8 @@ export async function getRealmScopedAuth(
386385
let response = await fetchImpl(`${normalizedUrl}_realm-auth`, {
387386
method: 'POST',
388387
headers: {
389-
Accept: 'application/json',
390-
'Content-Type': 'application/json',
388+
Accept: SupportedMimeType.JSON,
389+
'Content-Type': SupportedMimeType.JSON,
391390
Authorization: serverToken,
392391
},
393392
});
@@ -485,7 +484,7 @@ async function addRealmToMatrixAccountData(
485484
let putResponse = await fetchImpl(accountDataUrl, {
486485
method: 'PUT',
487486
headers: {
488-
'Content-Type': 'application/json',
487+
'Content-Type': SupportedMimeType.JSON,
489488
Authorization: `Bearer ${matrixAuth.accessToken}`,
490489
},
491490
body: JSON.stringify({ realms: existingRealms }),
@@ -523,10 +522,9 @@ export async function pullRealmFiles(
523522
let fetchImpl = options?.fetch ?? globalThis.fetch;
524523
let normalizedRealmUrl = ensureTrailingSlash(realmUrl);
525524

526-
// _mtimes requires Accept: application/vnd.api+json (SupportedMimeType.Mtimes)
527525
let headers = buildAuthHeaders(
528526
options?.authorization,
529-
'application/vnd.api+json',
527+
SupportedMimeType.JSONAPI,
530528
);
531529

532530
// Fetch mtimes to discover all file paths.
@@ -577,7 +575,10 @@ export async function pullRealmFiles(
577575
try {
578576
let fileResponse = await fetchImpl(fullUrl, {
579577
method: 'GET',
580-
headers: buildAuthHeaders(options?.authorization, cardSourceMimeType),
578+
headers: buildAuthHeaders(
579+
options?.authorization,
580+
SupportedMimeType.CardSource,
581+
),
581582
});
582583

583584
if (!fileResponse.ok) {

0 commit comments

Comments
 (0)