|
|
@@ -38,9 +38,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
appState, |
|
|
|
appParams, |
|
|
|
serverParams, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
}) => (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const data = { |
|
|
@@ -49,7 +49,7 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
serialized = responseBodyMediaType.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -63,7 +63,7 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
encoded = responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -76,9 +76,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
const theHeaders: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
}; |
|
|
|
|
|
|
|
const registeredResources = Array.from(appState.resources); |
|
|
@@ -107,9 +107,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
|
|
|
|
export const handleGetCollection: Middleware = ({ |
|
|
|
resource, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
backendState, |
|
|
|
query, |
|
|
@@ -148,7 +148,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
serialized = responseBodyMediaType.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -162,7 +162,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
encoded = responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -175,9 +175,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
}; |
|
|
|
|
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
@@ -195,9 +195,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
export const handleGetItem: Middleware = ({ |
|
|
|
resourceId, |
|
|
|
resource, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
}) => async (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
try { |
|
|
@@ -229,7 +229,7 @@ export const handleGetItem: Middleware = ({ |
|
|
|
|
|
|
|
let serialized: string | null; |
|
|
|
try { |
|
|
|
serialized = data === null ? null : responseBodySerializerPair.serialize(data); |
|
|
|
serialized = data === null ? null : responseBodyMediaType.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -243,7 +243,7 @@ export const handleGetItem: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = serialized === null ? null : encoding.encode(serialized); |
|
|
|
encoded = serialized === null ? null : responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -257,9 +257,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
|
|
|
|
if (encoded) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourceFetched(resource) |
|
|
|
res.end(encoded); |
|
|
@@ -353,12 +353,12 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
appState, |
|
|
|
resource, |
|
|
|
resourceId, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
errorResponseBodyMediaType, |
|
|
|
errorResponseBodyEncoding, |
|
|
|
}) => async (req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const { deserializerPair: requestBodyDeserializerPair, encodingPair: requestBodyEncodingPair } = getDeserializerObjects(appState, req); |
|
|
|
if (typeof requestBodyDeserializerPair === 'undefined' || typeof requestBodyEncodingPair === 'undefined') { |
|
|
@@ -440,16 +440,16 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
} |
|
|
|
// TODO better error reporting, localizable messages |
|
|
|
// TODO handle error handlers' errors |
|
|
|
const serialized = errorSerializerPair.serialize( |
|
|
|
const serialized = errorResponseBodyMediaType.serialize( |
|
|
|
err.issues.map((i) => ( |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorResponseBodyEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
'Content-Type': errorResponseBodyMediaType.name, |
|
|
|
'Content-Encoding': errorResponseBodyEncoding.name, |
|
|
|
}) |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.end(encoded); |
|
|
@@ -475,7 +475,7 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
serialized = responseBodyMediaType.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -489,7 +489,7 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
encoded = responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -502,9 +502,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourcePatched(resource); |
|
|
|
res.end(encoded); |
|
|
@@ -519,12 +519,12 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
appState, |
|
|
|
serverParams, |
|
|
|
backendState, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
errorResponseBodyMediaType, |
|
|
|
errorResponseBodyEncoding, |
|
|
|
resource, |
|
|
|
}) => async (req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const { deserializerPair: requestBodyDeserializerPair, encodingPair: requestBodyEncodingPair } = getDeserializerObjects(appState, req); |
|
|
@@ -562,16 +562,16 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
} |
|
|
|
// TODO better error reporting, localizable messages |
|
|
|
// TODO handle error handlers' errors |
|
|
|
const serialized = errorSerializerPair.serialize( |
|
|
|
const serialized = errorResponseBodyMediaType.serialize( |
|
|
|
err.issues.map((i) => ( |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorResponseBodyEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
'Content-Type': errorResponseBodyMediaType.name, |
|
|
|
'Content-Encoding': errorResponseBodyEncoding.name, |
|
|
|
}) |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(encoded); |
|
|
@@ -628,7 +628,7 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
serialized = responseBodyMediaType.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -642,7 +642,7 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
encoded = responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -655,9 +655,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
'Location': `${serverParams.baseUrl}/${resource.state.routeName}/${newId}` |
|
|
|
}; |
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
@@ -674,12 +674,12 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
export const handleEmplaceItem: Middleware = ({ |
|
|
|
appState, |
|
|
|
serverParams, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyMediaType, |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
responseBodyEncoding, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
errorResponseBodyMediaType, |
|
|
|
errorResponseBodyEncoding, |
|
|
|
resource, |
|
|
|
resourceId, |
|
|
|
backendState, |
|
|
@@ -731,16 +731,16 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
} |
|
|
|
// TODO better error reporting, localizable messages |
|
|
|
// TODO handle error handlers' errors |
|
|
|
const serialized = errorSerializerPair.serialize( |
|
|
|
const serialized = errorResponseBodyMediaType.serialize( |
|
|
|
err.issues.map((i) => ( |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorResponseBodyEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
'Content-Type': errorResponseBodyMediaType.name, |
|
|
|
'Content-Encoding': errorResponseBodyEncoding.name, |
|
|
|
}) |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(encoded); |
|
|
@@ -781,7 +781,7 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
serialized = responseBodyMediaType.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -795,7 +795,7 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
encoded = responseBodyEncoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
@@ -808,9 +808,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Type': responseBodyMediaType.name, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Content-Encoding': responseBodyEncoding.name, |
|
|
|
}; |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
if (backendState.showTotalItemCountOnCreateItem && typeof resource.dataSource.getTotalCount === 'function') { |
|
|
|