/* * © 2024 Snyk Limited * * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.instanceToJsonApiSource = void 0; const SOURCE_REGEXP = /^\/(query|params|body|headers)\/.*/; /** * Converts the instance string from an error into a JsonApiErrorSource * @param instance the instance string to convert * @returns A JsonApiErrorSource if appropriate, or undefined if not */ const instanceToJsonApiSource = (instance) => { let source; // See if the instance conforms to one of the source formats const matchErrorPath = instance.match(SOURCE_REGEXP); // If the match captures the path type and parameter, if (matchErrorPath !== null && matchErrorPath.length > 0) { // Convert the instance into a source switch (matchErrorPath[1]) { case 'query': case 'params': source = paramsInstanceToJsonApiSource(instance); break; case 'body': source = bodyInstanceToJsonApiSource(instance); break; case 'headers': source = headerInstanceToJsonApiSource(instance); break; } } return source; }; exports.instanceToJsonApiSource = instanceToJsonApiSource; // For a parameter, do not include the leading slash const PARAMETER_REGEXP = /^\/(?:params|query)\/(.*)/; const paramsInstanceToJsonApiSource = (instance) => { let parameter = ''; // The source is the name of the parameter, and is not a json path const matches = instance.match(PARAMETER_REGEXP); if (matches !== null && matches.length > 0) { parameter = matches[1]; } return { parameter, }; }; // For a pointer, capture the JSON path, including the leading slash const POINTER_REGEXP = /^\/body(\/.*)/; const bodyInstanceToJsonApiSource = (instance) => { let pointer = ''; const matches = instance.match(POINTER_REGEXP); if (matches !== null && matches.length > 0) { pointer = matches[1]; } return { pointer, }; }; // The source is the name of the header, and is not a json path const HEADER_REGEXP = /^\/headers\/(.*)/; const headerInstanceToJsonApiSource = (instance) => { let header = ''; const matches = instance.match(HEADER_REGEXP); if (matches !== null && matches.length > 0) { header = matches[1]; } return { header, }; }; //# sourceMappingURL=json-api.js.map