Opensearch dashboards.opensearchdashboardsresponsefactory
Home > opensearch-dashboards > opensearchDashboardsResponseFactory
opensearchDashboardsResponseFactory variable
Set of helpers used to create OpenSearchDashboardsResponse
to form HTTP response on an incoming request. Should be returned as a result of RequestHandler execution.
Signature:
opensearchDashboardsResponseFactory: {
custom: <T extends string | Error | Buffer | Stream | Record<string, any> | {
message: string | Error;
attributes?: Record<string, any> | undefined;
} | undefined>(options: CustomHttpResponseOptions<T>) => OpenSearchDashboardsResponse<T>;
badRequest: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
unauthorized: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
forbidden: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
notFound: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
conflict: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
internalError: (options?: ErrorHttpResponseOptions) => OpenSearchDashboardsResponse<ResponseError>;
customError: (options: CustomHttpResponseOptions<ResponseError>) => OpenSearchDashboardsResponse<ResponseError>;
redirected: (options: RedirectResponseOptions) => OpenSearchDashboardsResponse<string | Buffer | Stream | Record<string, any>>;
ok: (options?: HttpResponseOptions) => OpenSearchDashboardsResponse<string | Buffer | Stream | Record<string, any>>;
accepted: (options?: HttpResponseOptions) => OpenSearchDashboardsResponse<string | Buffer | Stream | Record<string, any>>;
noContent: (options?: HttpResponseOptions) => OpenSearchDashboardsResponse<undefined>;
}
Example
- Successful response. Supported types of response body are: -
undefined
, no content to send. -string
, send text -JSON
, send JSON object, HTTP server will throw if given object is not valid (has circular references, for example) -Stream
send data stream -Buffer
send binary stream
return response.ok();
return response.ok({ body: 'ack' });
return response.ok({ body: { id: '1' } });
return response.ok({ body: Buffer.from(...) });
const stream = new Stream.PassThrough();
fs.createReadStream('./file').pipe(stream);
return res.ok({ body: stream });
HTTP headers are configurable via response factory parameter options
HttpResponseOptions.
return response.ok({
body: { id: '1' },
headers: {
'content-type': 'application/json'
}
});
- Redirection response. Redirection URL is configures via 'Location' header.
return response.redirected({
body: 'The document has moved',
headers: {
location: '/new-url',
},
});
- Error response. You may pass an error message to the client, where error message can be: -
string
send message text -Error
send the message text of given Error object. -{ message: string | Error, attributes: {data: Record<string, any>, ...} }
- send message text and attach additional error data.
return response.unauthorized({
body: 'User has no access to the requested resource.',
headers: {
'WWW-Authenticate': 'challenge',
}
})
return response.badRequest();
return response.badRequest({ body: 'validation error' });
try {
// ...
} catch(error){
return response.badRequest({ body: error });
}
return response.badRequest({
body:{
message: 'validation error',
attributes: {
requestBody: request.body,
failedFields: validationResult
}
}
});
try {
// ...
} catch(error) {
return response.badRequest({
body: error
});
}
- Custom response.
ResponseFactory
may not cover your use case, so you can use thecustom
function to customize the response.
return response.custom({
body: 'ok',
statusCode: 201,
headers: {
location: '/created-url'
}
})