"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMethod = exports.genGateway = exports.Pattern = void 0; const fs_1 = require("fs"); const ts_poet_1 = require("ts-poet"); const path_to_regexp_1 = require("path-to-regexp"); const Gateway = ts_poet_1.imp("Gateway@twirp-ts"); const GatewayPattern = ts_poet_1.imp("Pattern@twirp-ts"); const pathToRegexpMatch = ts_poet_1.imp("match@path-to-regexp"); const debug = (content) => fs_1.writeFileSync(__dirname + "/debug.json", JSON.stringify(content, null, 2), "utf-8"); var Pattern; (function (Pattern) { Pattern["POST"] = "post"; Pattern["GET"] = "get"; Pattern["PATCH"] = "patch"; Pattern["PUT"] = "put"; Pattern["DELETE"] = "delete"; })(Pattern = exports.Pattern || (exports.Pattern = {})); function genGateway(ctx, files) { return __awaiter(this, void 0, void 0, function* () { const httpRoutes = files.reduce((all, current) => { current.service.forEach(service => { service.method.forEach((method) => { const options = ctx.interpreter.readOptions(method); if (options && options["google.api.http"]) { const httpSpec = options["google.api.http"]; all.push(parseHttpOption(httpSpec, current.package || "", method.name, service.name)); if (httpSpec.additional_bindings) { all.push(parseHttpOption(httpSpec.additional_bindings, current.package || "", method.name, service.name)); } } }); }); return all; }, []); return genGatewayHandler(httpRoutes).toStringWithImports(); }); } exports.genGateway = genGateway; function genGatewayHandler(httpRoute) { const genRoutes = (method) => httpRoute.filter(route => route.httpMethod === method).map(route => { return ts_poet_1.code ` { packageName: "${route.packageName}", methodName: "${route.methodName}", serviceName: "${route.serviceName}", httpMethod: "${route.httpMethod}" as ${GatewayPattern}, matchingPath: "${route.matchingPath}{:query_string(\\\\?.*)}?", matcher: ${pathToRegexpMatch}("${route.matchingPath}{:query_string(\\\\?.*)}?"), bodyKey: "${route.bodyKey || ""}", responseBodyKey: "${route.responseBodyKey || ""}", }, `; }); return ts_poet_1.code ` export function createGateway() { return new ${Gateway}({ post: [${ts_poet_1.joinCode(genRoutes(Pattern.POST), { on: "\n" })}], get: [${ts_poet_1.joinCode(genRoutes(Pattern.GET), { on: "\n" })}], put: [${ts_poet_1.joinCode(genRoutes(Pattern.PUT), { on: "\n" })}], patch: [${ts_poet_1.joinCode(genRoutes(Pattern.PATCH), { on: "\n" })}], delete: [${ts_poet_1.joinCode(genRoutes(Pattern.DELETE), { on: "\n" })}], }) } `; } function parseHttpOption(httpOption, packageName, methodName, serviceName) { const httpMethod = getMethod(httpOption); const matchingUrl = httpOption[httpMethod]; const matchingPath = matcher(matchingUrl); const httpRoute = { packageName, methodName, serviceName, httpMethod: httpMethod, matchingPath, matcher: path_to_regexp_1.match(matchingPath), bodyKey: httpOption.body, responseBodyKey: httpOption.responseBody, }; return httpRoute; } function matcher(url) { return url.split("/").map((urlSegment) => { const matchURLParams = /{([0-9a-zA-Z_-]+)}/.exec(urlSegment); if (matchURLParams && matchURLParams.length > 0) { const paramName = matchURLParams[1]; return "{:" + paramName + "}"; } else { return urlSegment; } }).join("/"); } function getMethod(httpSpec) { const possibleMethods = ["post", "get", "patch", "put", "delete"]; for (const method of possibleMethods) { if (method in httpSpec) { return method; } } throw new Error(`HTTP method not found`); } exports.getMethod = getMethod;