|
|
@@ -43,13 +43,13 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
}) => (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
const singleResDatum = { |
|
|
|
const data = { |
|
|
|
name: appParams.name |
|
|
|
}; |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(singleResDatum); |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -61,9 +61,9 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let theFormatted; |
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
theFormatted = encoding.encode(serialized); |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -99,7 +99,7 @@ export const handleGetRoot: Middleware = ({ |
|
|
|
} |
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, theHeaders); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.ok(); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
@@ -112,6 +112,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
responseBodyEncoding: [encodingKey, encoding], |
|
|
|
errorResponseBodyLanguage: [errorLanguageCode, errorMessageCollection], |
|
|
|
backendState, |
|
|
|
query, |
|
|
|
}) => async (_req: IncomingMessage, res: ServerResponse) => { |
|
|
|
try { |
|
|
|
await resource.dataSource.initialize(); |
|
|
@@ -126,13 +127,13 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let resData: Object[]; |
|
|
|
let data: v.Output<typeof resource.schema>[]; |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
try { |
|
|
|
// TODO querying mechanism |
|
|
|
resData = await resource.dataSource.getMultiple(); // TODO paginated responses per resource |
|
|
|
data = await resource.dataSource.getMultiple(query); // TODO paginated responses per resource |
|
|
|
if (backendState.showTotalItemCountOnGetCollection && typeof resource.dataSource.getTotalCount === 'function') { |
|
|
|
totalItemCount = await resource.dataSource.getTotalCount(); |
|
|
|
totalItemCount = await resource.dataSource.getTotalCount(query); |
|
|
|
} |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
@@ -147,7 +148,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(resData); |
|
|
|
serialized = responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -159,9 +160,9 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let theFormatted; |
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
theFormatted = encoding.encode(serialized); |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -185,7 +186,7 @@ export const handleGetCollection: Middleware = ({ |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, headers); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceCollectionFetched(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
@@ -212,9 +213,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let singleResDatum: Object | null = null; |
|
|
|
let data: v.Output<typeof resource.schema> | null = null; |
|
|
|
try { |
|
|
|
singleResDatum = await resource.dataSource.getSingle(resourceId); |
|
|
|
data = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -228,7 +229,7 @@ export const handleGetItem: Middleware = ({ |
|
|
|
|
|
|
|
let serialized: string | null; |
|
|
|
try { |
|
|
|
serialized = singleResDatum === null ? null : responseBodySerializerPair.serialize(singleResDatum); |
|
|
|
serialized = data === null ? null : responseBodySerializerPair.serialize(data); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -240,9 +241,9 @@ export const handleGetItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let theFormatted; |
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
theFormatted = serialized === null ? null : encoding.encode(serialized); |
|
|
|
encoded = serialized === null ? null : encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -254,14 +255,14 @@ export const handleGetItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (theFormatted) { |
|
|
|
if (encoded) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_OK, { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceFetched(resource) |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
@@ -298,50 +299,50 @@ export const handleDeleteItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let response; |
|
|
|
let existing: unknown | null; |
|
|
|
try { |
|
|
|
response = await resource.dataSource.delete(resourceId); |
|
|
|
existing = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeleteResource(resource); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToFetchResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
const throwOnNotFound = !response && backendState.throws404OnDeletingNotFound; |
|
|
|
|
|
|
|
res.writeHead( |
|
|
|
throwOnNotFound |
|
|
|
? constants.HTTP_STATUS_NOT_FOUND |
|
|
|
: constants.HTTP_STATUS_NO_CONTENT, |
|
|
|
|
|
|
|
throwOnNotFound |
|
|
|
? { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
// TODO provide more details |
|
|
|
} |
|
|
|
: { |
|
|
|
'Content-Language': languageCode, |
|
|
|
} |
|
|
|
); |
|
|
|
res.statusMessage = ( |
|
|
|
throwOnNotFound |
|
|
|
? errorMessageCollection.statusMessages.deleteNonExistingResource(resource) |
|
|
|
: responseBodyMessageCollection.statusMessages.resourceDeleted(resource) |
|
|
|
); |
|
|
|
if (!existing && backendState.throws404OnDeletingNotFound) { |
|
|
|
res.writeHead(constants.HTTP_STATUS_NOT_FOUND, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.deleteNonExistingResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (throwOnNotFound) { |
|
|
|
// TODO provide error message |
|
|
|
try { |
|
|
|
if (existing) { |
|
|
|
await resource.dataSource.delete(resourceId); |
|
|
|
} |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToDeleteResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(constants.HTTP_STATUS_NO_CONTENT, { |
|
|
|
'Content-Language': languageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceDeleted(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
@@ -384,9 +385,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let existing: object | null; |
|
|
|
let existing: unknown | null; |
|
|
|
try { |
|
|
|
existing = await resource.dataSource.getSingle(resourceId); |
|
|
|
existing = await resource.dataSource.getById(resourceId); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -411,7 +412,7 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
|
|
|
|
let bodyDeserialized: unknown; |
|
|
|
try { |
|
|
|
const schema = resource.schema.type === 'object' ? resource.schema as v.ObjectSchema<any> : resource.schema |
|
|
|
const schema = resource.schema.type === 'object' ? resource.schema as unknown as v.ObjectSchema<any> : resource.schema |
|
|
|
bodyDeserialized = await getBody( |
|
|
|
req, |
|
|
|
requestBodyDeserializerPair, |
|
|
@@ -444,22 +445,21 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const theFormatted = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResourcePatch(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
const params = bodyDeserialized as Record<string, unknown>; |
|
|
|
|
|
|
|
let newObject: object | null; |
|
|
|
let newObject: v.Output<typeof resource.schema> | null; |
|
|
|
try { |
|
|
|
newObject = await resource.dataSource.patch(resourceId, params); |
|
|
|
} catch { |
|
|
@@ -487,9 +487,9 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let theFormatted; |
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
theFormatted = encoding.encode(serialized); |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
@@ -507,7 +507,7 @@ export const handlePatchItem: Middleware = ({ |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourcePatched(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
@@ -567,14 +567,14 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const theFormatted = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
@@ -593,37 +593,79 @@ export const handleCreateItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//v.Output<typeof resource.schema> |
|
|
|
|
|
|
|
let newId; |
|
|
|
let params: v.Output<typeof resource.schema>; |
|
|
|
try { |
|
|
|
// TODO error handling for each process |
|
|
|
const newId = await resource.newId(resource.dataSource); |
|
|
|
const params = bodyDeserialized as Record<string, unknown>; |
|
|
|
newId = await resource.newId(resource.dataSource); |
|
|
|
params = bodyDeserialized as Record<string, unknown>; |
|
|
|
params[resource.state.idAttr] = newId; |
|
|
|
const newObject = await resource.dataSource.create(params); |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToGenerateIdFromResourceDataSource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
// noop |
|
|
|
// TODO |
|
|
|
} |
|
|
|
|
|
|
|
let newObject; |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
try { |
|
|
|
newObject = await resource.dataSource.create(params); |
|
|
|
if (backendState.showTotalItemCountOnCreateItem && typeof resource.dataSource.getTotalCount === 'function') { |
|
|
|
totalItemCount = await resource.dataSource.getTotalCount(); |
|
|
|
} |
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Location': `${serverParams.baseUrl}/${resource.state.routeName}/${newId}` |
|
|
|
} catch { |
|
|
|
// noop |
|
|
|
// TODO |
|
|
|
} |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
|
headers['X-Resource-Total-Item-Count'] = totalItemCount.toString(); |
|
|
|
} |
|
|
|
const serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
const theFormatted = encoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_CREATED, headers); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceCreated(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
} |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}) |
|
|
|
res.statusMessage = `Could Not Return ${resource.state.itemName}`; |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
'Location': `${serverParams.baseUrl}/${resource.state.routeName}/${newId}` |
|
|
|
}; |
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
|
headers['X-Resource-Total-Item-Count'] = totalItemCount.toString(); |
|
|
|
} |
|
|
|
res.writeHead(constants.HTTP_STATUS_CREATED, headers); |
|
|
|
res.statusMessage = responseBodyMessageCollection.statusMessages.resourceCreated(resource); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
@@ -656,7 +698,7 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
|
|
|
|
let bodyDeserialized: unknown; |
|
|
|
try { |
|
|
|
const schema = resource.schema.type === 'object' ? resource.schema as v.ObjectSchema<any> : resource.schema |
|
|
|
const schema = resource.schema.type === 'object' ? resource.schema as unknown as v.ObjectSchema<any> : resource.schema |
|
|
|
bodyDeserialized = await getBody( |
|
|
|
req, |
|
|
|
requestBodyDeserializerPair, |
|
|
@@ -694,14 +736,14 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
`${i.path?.map((p) => p.key)?.join('.') ?? i.reason}: ${i.message}` |
|
|
|
)), |
|
|
|
); |
|
|
|
const theFormatted = errorEncoding.encode(serialized); |
|
|
|
const encoded = errorEncoding.encode(serialized); |
|
|
|
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, { |
|
|
|
...headers, |
|
|
|
'Content-Type': errorMediaType, |
|
|
|
'Content-Encoding': errorEncodingKey, |
|
|
|
}) |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.invalidResource(resource); |
|
|
|
res.end(theFormatted); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
@@ -720,40 +762,73 @@ export const handleEmplaceItem: Middleware = ({ |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let newObject: v.Output<typeof resource.schema>; |
|
|
|
let isCreated: boolean; |
|
|
|
try { |
|
|
|
// TODO error handling for each process |
|
|
|
const params = bodyDeserialized as Record<string, unknown>; |
|
|
|
params[resource.state.idAttr] = resource.state.idConfig.deserialize(params[resource.state.idAttr] as string); |
|
|
|
const [newObject, isCreated] = await resource.dataSource.emplace(resourceId, params); |
|
|
|
const serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
const theFormatted = encoding.encode(serialized); |
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
[newObject, isCreated] = await resource.dataSource.emplace(resourceId, params); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEmplaceResource(resource); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
if (backendState.showTotalItemCountOnCreateItem && typeof resource.dataSource.getTotalCount === 'function') { |
|
|
|
totalItemCount = await resource.dataSource.getTotalCount(); |
|
|
|
} |
|
|
|
if (isCreated) { |
|
|
|
headers['Location'] = `${serverParams.baseUrl}/${resource.state.routeName}/${resourceId}`; |
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
|
headers['X-Resource-Total-Item-Count'] = totalItemCount.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
res.writeHead(isCreated ? constants.HTTP_STATUS_CREATED : constants.HTTP_STATUS_OK, headers); |
|
|
|
res.statusMessage = ( |
|
|
|
isCreated |
|
|
|
? responseBodyMessageCollection.statusMessages.resourceCreated(resource) |
|
|
|
: responseBodyMessageCollection.statusMessages.resourceReplaced(resource) |
|
|
|
); |
|
|
|
res.end(theFormatted); |
|
|
|
} |
|
|
|
|
|
|
|
let serialized; |
|
|
|
try { |
|
|
|
serialized = responseBodySerializerPair.serialize(newObject); |
|
|
|
} catch { |
|
|
|
res.statusCode = constants.HTTP_STATUS_INTERNAL_SERVER_ERROR; |
|
|
|
res.statusMessage = `Could Not Return ${resource.state.itemName}`; |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToSerializeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
let encoded; |
|
|
|
try { |
|
|
|
encoded = encoding.encode(serialized); |
|
|
|
} catch { |
|
|
|
res.writeHead(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, { |
|
|
|
'Content-Language': errorLanguageCode, |
|
|
|
}); |
|
|
|
res.statusMessage = errorMessageCollection.statusMessages.unableToEncodeResponse(); |
|
|
|
res.end(); |
|
|
|
return { |
|
|
|
handled: true, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
const headers: Record<string, string> = { |
|
|
|
'Content-Type': responseBodyMediaType, |
|
|
|
'Content-Language': languageCode, |
|
|
|
'Content-Encoding': encodingKey, |
|
|
|
}; |
|
|
|
let totalItemCount: number | undefined; |
|
|
|
if (backendState.showTotalItemCountOnCreateItem && typeof resource.dataSource.getTotalCount === 'function') { |
|
|
|
totalItemCount = await resource.dataSource.getTotalCount(); |
|
|
|
} |
|
|
|
if (isCreated) { |
|
|
|
headers['Location'] = `${serverParams.baseUrl}/${resource.state.routeName}/${resourceId}`; |
|
|
|
if (typeof totalItemCount !== 'undefined') { |
|
|
|
headers['X-Resource-Total-Item-Count'] = totalItemCount.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
res.writeHead(isCreated ? constants.HTTP_STATUS_CREATED : constants.HTTP_STATUS_OK, headers); |
|
|
|
res.statusMessage = ( |
|
|
|
isCreated |
|
|
|
? responseBodyMessageCollection.statusMessages.resourceCreated(resource) |
|
|
|
: responseBodyMessageCollection.statusMessages.resourceReplaced(resource) |
|
|
|
); |
|
|
|
res.end(encoded); |
|
|
|
return { |
|
|
|
handled: true |
|
|
|
}; |
|
|
|