UNPKG

3.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const errors_1 = require("../errors");
4const http_1 = require("../http");
5const util_1 = require("../util");
6const parser_helpers_1 = require("./parser-helpers");
7const type_parser_1 = require("./type-parser");
8function parseQueryParams(parameter, typeTable, lociTable) {
9 var _a;
10 parameter.getDecoratorOrThrow("queryParams");
11 if (parameter.hasQuestionToken()) {
12 return util_1.err(new errors_1.OptionalNotAllowedError("@queryParams parameter cannot be optional", {
13 file: parameter.getSourceFile().getFilePath(),
14 position: parameter.getQuestionTokenNodeOrThrow().getPos()
15 }));
16 }
17 const queryParamTypeLiteral = parser_helpers_1.getParameterTypeAsTypeLiteralOrThrow(parameter);
18 const queryParams = [];
19 for (const propertySignature of queryParamTypeLiteral.getProperties()) {
20 const nameResult = extractQueryParamName(propertySignature);
21 if (nameResult.isErr())
22 return nameResult;
23 const name = nameResult.unwrap();
24 const typeResult = extractQueryParamType(propertySignature, typeTable, lociTable);
25 if (typeResult.isErr())
26 return typeResult;
27 const type = typeResult.unwrap();
28 const description = (_a = parser_helpers_1.getJsDoc(propertySignature)) === null || _a === void 0 ? void 0 : _a.getDescription().trim();
29 const optional = propertySignature.hasQuestionToken();
30 queryParams.push({ name, type, description, optional });
31 }
32 // TODO: add loci information
33 return util_1.ok(queryParams.sort((a, b) => (b.name > a.name ? -1 : 1)));
34}
35exports.parseQueryParams = parseQueryParams;
36function extractQueryParamName(propertySignature) {
37 const name = parser_helpers_1.getPropertyName(propertySignature);
38 if (!/^[\w-]*$/.test(name)) {
39 return util_1.err(new errors_1.ParserError("@queryParams property name may only contain alphanumeric, underscore and hyphen characters", {
40 file: propertySignature.getSourceFile().getFilePath(),
41 position: propertySignature.getPos()
42 }));
43 }
44 if (name.length === 0) {
45 return util_1.err(new errors_1.ParserError("@queryParams property name must not be empty", {
46 file: propertySignature.getSourceFile().getFilePath(),
47 position: propertySignature.getPos()
48 }));
49 }
50 return util_1.ok(name);
51}
52function extractQueryParamType(propertySignature, typeTable, lociTable) {
53 const typeResult = type_parser_1.parseType(propertySignature.getTypeNodeOrThrow(), typeTable, lociTable);
54 if (typeResult.isErr())
55 return typeResult;
56 if (!http_1.isQueryParamTypeSafe(typeResult.unwrap(), typeTable)) {
57 return util_1.err(new errors_1.ParserError("query parameter type may only be a URL-safe type, a single depth object of URL-safe types or an array of URL-safe types", {
58 file: propertySignature.getSourceFile().getFilePath(),
59 position: propertySignature.getPos()
60 }));
61 }
62 return typeResult;
63}