This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
78 lines (73 loc) · 2.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// This file is part of the @egomobile/api-utils distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// @egomobile/api-utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// @egomobile/api-utils is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import type { IHttpRequest, IHttpResponse } from "@egomobile/http-server";
import type { Nilable } from "../types/internal";
import { ApiResponseBuilder } from "./classes/ApiResponseBuilder";
/**
* Options for 'apiResponse()' function.
*/
export interface IApiResponseOptions {
/**
* Execute 'end()' method of response
* when invoke 'send()' of ApiResponseBuilder.
*/
executeEnd?: Nilable<boolean>;
}
/**
* Creates a new instance, building the data for an API response.
*
* @example
* ```
* import createServer from '@egomobile/http-server'
* import { apiResponse } from '@egomobile/api-utils'
*
* const app = createServer()
*
* app.get('/', async (request, response) => {
* // create a new object and write
* // it to 'newObject'
*
* apiResponse(request, response)
* .withStatus(201) // set HTTP status code to 201
* .withData(newObject) // set value for 'data' prop
* .addMessage('Object has been created') // a simple 'info' message for the user
* .addMessage({
* type: 'warn'
* internal: true,
* message: 'A new object with ID ' + newObject.id + ' has been created'
* }) // internal warning message
* .send() // write all data to client
* })
*
* app.listen()
* .catch(console.error)
* ```
*
* @param {IHttpRequest} request The response context.
* @param {IHttpResponse} response The response context.
* @param {Nilable<IApiResponseOptions>} [options] Custom options.
*
* @returns {ApiResponseBuilder} The new instance.
*/
export function apiResponse(request: IHttpRequest, response: IHttpResponse, options?: Nilable<IApiResponseOptions>): ApiResponseBuilder {
return new ApiResponseBuilder({
request,
response,
"executeEnd": options?.executeEnd
});
}
export * from "./classes";
export * from "./handlers";
export * from "./swagger";