@sauce-api/core
Version:
Sauce API core functionality
142 lines (141 loc) • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
/** Configure a route for the application */
class Route {
constructor() {
this.queryParamKeys = [];
this.isDeprecated = false;
}
setMethod(method) {
this.method = method.toString().toLocaleLowerCase();
return this;
}
setPath(path) {
this.path = path;
return this;
}
// Mutually exclusive to this.controller
// Controller is still expected to be in the api folder
setCustomControllerPath(customControllerPath) {
this.customControllerPath = customControllerPath;
return this;
}
// Mutually exclusive to this.customControllerPath
setController(controller) {
this.controller = controller;
return this;
}
setAction(action) {
this.action = action;
return this;
}
setPolicies(policies) {
this.policies = policies;
return this;
}
setDescription(description) {
this.description = description;
return this;
}
setTag(tag) {
this.tag = tag;
return this;
}
setPathParams(params) {
this.pathParams = params;
this.formattedPathParams = this.formatParams(params);
return this;
}
setQueryParams(queryParams) {
this.queryParams = queryParams;
this.formattedQueryParams = this.formatParams(queryParams);
this.queryParams.forEach((param) => {
this.queryParamKeys.push(param.name);
});
return this;
}
setBodySchema(bodySchema) {
this.bodySchema = bodySchema;
this.formattedBodySchema = this.buildSchema(bodySchema);
return this;
}
setExcludedEnvironments(environments) {
this.excludedEnvironments = environments;
return this;
}
setHidden(hidden) {
this.hidden = hidden;
return this;
}
setExampleResponse(exampleResponse) {
this.exampleResponse = exampleResponse;
return this;
}
setIsDeprecated(value) {
this.isDeprecated = value;
return this;
}
getFormattedPathParams() {
return this.formattedPathParams;
}
getFormattedQueryParams() {
return this.formattedQueryParams;
}
getFormattedBodySchema() {
return this.formattedBodySchema;
}
/**
* A shortcut for determining if a route has
* a specific query param configured for it.
*
* IT WILL NOT DETECT IF THE REQUEST HAS A QUERY
* PARAM PRESENT
*
* @param {sting} key - the query param key to check
* @return {boolean}
*/
hasQueryParam(key) {
return this.queryParamKeys.includes(key);
}
formatParams(queryParams) {
return queryParams.map((param) => {
return this.formatParam(param);
});
}
buildSchema(level) {
const schema = {};
level.forEach((item) => {
schema[item.name] = this.buildBodySchemaLevel(item);
});
return schema;
}
buildBodySchemaLevel(item) {
let obj = {};
if (item.children) {
if (item.type === "object") {
obj = this.buildSchema(item.children);
}
else {
obj = [this.buildSchema(item.children)];
}
}
else {
obj = this.formatParam(item);
}
return obj;
}
formatParam(item) {
const obj = {
name: item.name,
description: item.description,
required: item.required || false,
type: item.type
};
if (item.type === "enum") {
obj["enumValues"] = item.enumValues;
}
return obj;
}
}
exports.Route = Route;