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