@@ -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-
4743export 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