acady-api-builder
Version:
Acady API Builder
59 lines • 2.25 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressConverter = void 0;
const api_headers_1 = require("../core/api-headers");
const url_1 = require("url");
class ExpressConverter {
static convertRequest(event) {
var _a, _b;
const protocol = event.protocol;
const hostname = event.get('host');
const fullUrl = protocol + '://' + hostname + event.originalUrl;
const url = new url_1.URL(fullUrl);
const headers = new api_headers_1.ApiHeaders();
const endpoint = protocol + '://' + hostname;
Object.keys(event.headers).forEach((key) => {
let value = event.headers[key];
headers.append(key, value);
});
const queryParams = {};
for (let key of url.searchParams.keys()) {
queryParams[key] = url.searchParams.get(key);
}
const request = {
method: event.method,
hostname: event.hostname,
headers: headers,
pathName: url.pathname,
queryParams: queryParams,
routePath: null,
pathParams: null,
endpoint: endpoint,
fullUrl: fullUrl,
body: undefined,
rawBody: undefined,
};
if (event.body) {
let body = event.body;
if (Buffer.isBuffer(body))
body = body.toString('utf8');
if (((_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a.getValue('content-type')) === null || _b === void 0 ? void 0 : _b.startsWith('application/json')) &&
typeof body === 'string') {
body = JSON.parse(body);
}
request.body = body;
request.rawBody = event.body;
}
return request;
}
static convertResponse(acadyApiResponse, response) {
response.status(acadyApiResponse.status);
for (let header of acadyApiResponse.headers.entries()) {
response.append(header.key, header.value);
}
response.send(acadyApiResponse.body);
}
}
exports.ExpressConverter = ExpressConverter;
ExpressConverter.TYPE = 'EXPRESS';
//# sourceMappingURL=express-converter.js.map
;