|
|
@@ -5,15 +5,15 @@ import {getBody, getDeserializerObjects} from './utils'; |
|
|
|
import {IncomingMessage, ServerResponse} from 'http'; |
|
|
|
|
|
|
|
export const handleHasMethodAndUrl: Middleware = ({ |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
}) => (req: IncomingMessage, res: ServerResponse) => { |
|
|
|
if (!req.method) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_METHOD_NOT_ALLOWED, { |
|
|
|
'Allow': 'HEAD, GET, POST, PUT, PATCH, DELETE', |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
|
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.methodNotAllowed(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.methodNotAllowed(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -22,7 +22,7 @@ export const handleHasMethodAndUrl: Middleware = ({ |
|
|
|
|
|
|
|
if (!req.url) { |
|
|
|
res.statusCode = constants.HTTP_STATUS_BAD_REQUEST; |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.badRequest(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.badRequest(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -39,9 +39,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
appParams, |
|
|
|
serverParams, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
}) => (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const data = { |
|
|
|
name: appParams.name |
|
|
@@ -52,9 +52,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -66,9 +66,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -77,7 +77,7 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
|
|
|
|
const theHeaders: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}; |
|
|
|
|
|
|
@@ -91,14 +91,14 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
// we are using custom headers for links because the standard Link header |
|
|
|
// is referring to the document metadata (e.g. author, next page, etc) |
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link |
|
|
|
theHeaders['X-Resource-Link'] = availableResources |
|
|
|
theHeaders['Link'] = availableResources |
|
|
|
.map((r) => |
|
|
|
`<${serverParams.baseUrl}/${r.state.routeName}>; name="${r.state.collectionName}"`, |
|
|
|
`<${serverParams.baseUrl}/${r.state.routeName}>; rel="related"; name="${r.state.collectionName}"`, |
|
|
|
) |
|
|
|
.join(', '); |
|
|
|
} |
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, theHeaders); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.ok(); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.ok(); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -108,9 +108,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
export const handleGetCollection: Middleware = ({ |
|
|
|
resource, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
backendState, |
|
|
|
query, |
|
|
|
}) => async (_req: IncomingMessage, res: ServerResponse) => { |
|
|
@@ -118,9 +118,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -137,9 +137,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
} |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToFetchResourceCollection(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToFetchResourceCollection(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -151,9 +151,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -165,9 +165,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -176,7 +176,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}; |
|
|
|
|
|
|
@@ -185,7 +185,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, headers); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceCollectionFetched(resource); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourceCollectionFetched(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -196,17 +196,17 @@ export const handleGetItem: Middleware = ({ |
|
|
|
resourceId, |
|
|
|
resource, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
}) => async (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
try { |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -218,9 +218,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
data = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToFetchResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToFetchResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -232,9 +232,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
serialized = data === null ? null : responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -246,9 +246,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
encoded = serialized === null ? null : encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -258,10 +258,10 @@ export const handleGetItem: Middleware = ({ |
|
|
|
if (encoded) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceFetched(resource) |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourceFetched(resource) |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -269,9 +269,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_NOT_FOUND, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.resourceNotFound(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.resourceNotFound(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -282,17 +282,17 @@ export const handleGetItem: Middleware = ({ |
|
|
|
export const handleDeleteItem: Middleware = ({ |
|
|
|
resource, |
|
|
|
resourceId, |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
errorResponseBodyLanguage, |
|
|
|
backendState, |
|
|
|
}) => async (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
try { |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -304,9 +304,9 @@ export const handleDeleteItem: Middleware = ({ |
|
|
|
existing = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToFetchResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToFetchResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -315,9 +315,9 @@ export const handleDeleteItem: Middleware = ({ |
|
|
|
|
|
|
|
if (!existing && backendState.throws404OnDeletingNotFound) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_NOT_FOUND, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.deleteNonExistingResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.deleteNonExistingResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -330,9 +330,9 @@ export const handleDeleteItem: Middleware = ({ |
|
|
|
} |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeleteResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToDeleteResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -340,9 +340,9 @@ export const handleDeleteItem: Middleware = ({ |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_NO_CONTENT, { |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceDeleted(resource); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourceDeleted(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -354,18 +354,18 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
resource, |
|
|
|
resourceId, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
}) => async (req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const { deserializerPair: requestBodyDeserializerPair, encodingPair: requestBodyEncodingPair } = getDeserializerObjects(appState, req); |
|
|
|
if (typeof requestBodyDeserializerPair === 'undefined' || typeof requestBodyEncodingPair === 'undefined') { |
|
|
|
res.writeHead(constants.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -376,9 +376,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -390,9 +390,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
existing = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToFetchResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToFetchResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -401,9 +401,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
|
|
|
|
if (!existing) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_NOT_FOUND, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.patchNonExistingResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.patchNonExistingResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -428,11 +428,11 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
} catch (errRaw) { |
|
|
|
const err = errRaw as v.ValiError; |
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
}; |
|
|
|
if (!Array.isArray(err.issues)) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, headers) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -451,7 +451,7 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -464,9 +464,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
newObject = await resource.dataSource.patch(resourceId, params); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToPatchResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToPatchResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -478,9 +478,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -492,9 +492,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -503,10 +503,10 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourcePatched(resource); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourcePatched(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -520,9 +520,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
serverParams, |
|
|
|
backendState, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
resource, |
|
|
@@ -530,9 +530,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
const { deserializerPair: requestBodyDeserializerPair, encodingPair: requestBodyEncodingPair } = getDeserializerObjects(appState, req); |
|
|
|
if (typeof requestBodyDeserializerPair === 'undefined' || typeof requestBodyEncodingPair === 'undefined') { |
|
|
|
res.writeHead(constants.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -550,11 +550,11 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
} catch (errRaw) { |
|
|
|
const err = errRaw as v.ValiError; |
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}; |
|
|
|
if (!Array.isArray(err.issues)) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, headers) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -573,7 +573,7 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -584,9 +584,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -603,9 +603,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
params[resource.state.idAttr] = newId; |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToGenerateIdFromResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToGenerateIdFromResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -631,9 +631,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -645,9 +645,9 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -656,7 +656,7 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Location': `${serverParams.baseUrl}/${resource.state.routeName}/${newId}` |
|
|
|
}; |
|
|
@@ -664,7 +664,7 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
headers['X-Resource-Total-Item-Count'] = totalItemCount.toString(); |
|
|
|
} |
|
|
|
res.writeHead(constants.HTTP_STATUS_CREATED, headers); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceCreated(resource); |
|
|
|
res.statusMessage = responseBodyLanguage.statusMessages.resourceCreated(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -675,9 +675,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
appState, |
|
|
|
serverParams, |
|
|
|
responseBodyMediaType: [responseBodyMediaType, responseBodySerializerPair], |
|
|
|
responseBodyLanguage: [languageCode, responseBodyMessageCollection], |
|
|
|
responseBodyLanguage, |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
errorResponseBodyLanguage, |
|
|
|
errorResponseBodyMediaType: [errorMediaType, errorSerializerPair], |
|
|
|
errorResponseBodyEncoding: [errorEncodingKey, errorEncoding], |
|
|
|
resource, |
|
|
@@ -687,9 +687,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
const { deserializerPair: requestBodyDeserializerPair, encodingPair: requestBodyEncodingPair } = getDeserializerObjects(appState, req); |
|
|
|
if (typeof requestBodyDeserializerPair === 'undefined' || typeof requestBodyEncodingPair === 'undefined') { |
|
|
|
res.writeHead(constants.HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToDeserializeRequest(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -719,11 +719,11 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
} catch (errRaw) { |
|
|
|
const err = errRaw as v.ValiError; |
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}; |
|
|
|
if (!Array.isArray(err.issues)) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, headers) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -742,7 +742,7 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.invalidResource(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -753,9 +753,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
await resource.dataSource.initialize(); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToInitializeResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -770,9 +770,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
[newObject, isCreated] = await resource.dataSource.emplace(resourceId, params); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEmplaceResource(resource); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEmplaceResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -784,9 +784,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -798,9 +798,9 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
'Content-Language': errorResponseBodyLanguage.name, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.statusMessage = errorResponseBodyLanguage.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
@@ -809,7 +809,7 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Language': responseBodyLanguage.name, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}; |
|
|
|
let totalItemCount: number | undefined; |
|
|
@@ -825,8 +825,8 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
res.writeHead(isCreated ? constants.HTTP_STATUS_CREATED : constants.HTTP_STATUS_OK, headers); |
|
|
|
res.statusMessage = ( |
|
|
|
isCreated |
|
|
|
? responseBodyMessageCollection.statusMessages.resourceCreated(resource) |
|
|
|
: responseBodyMessageCollection.statusMessages.resourceReplaced(resource) |
|
|
|
? responseBodyLanguage.statusMessages.resourceCreated(resource) |
|
|
|
: responseBodyLanguage.statusMessages.resourceReplaced(resource) |
|
|
|
); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|