| import { addQueryParameters } from "./util/add-query-parameters"; | |
| import { extractUrlVariableNames } from "./util/extract-url-variable-names"; | |
| import { omit } from "./util/omit"; | |
| import { parseUrl } from "./util/url-template"; | |
| function parse(options) { | |
| let method = options.method.toUpperCase(); | |
| let url = (options.url || "/").replace(/:([a-z]\w+)/g, "{$1}"); | |
| let headers = Object.assign({}, options.headers); | |
| let body; | |
| let parameters = omit(options, [ | |
| "method", | |
| "baseUrl", | |
| "url", | |
| "headers", | |
| "request", | |
| "mediaType" | |
| ]); | |
| const urlVariableNames = extractUrlVariableNames(url); | |
| url = parseUrl(url).expand(parameters); | |
| if (!/^http/.test(url)) { | |
| url = options.baseUrl + url; | |
| } | |
| const omittedParameters = Object.keys(options).filter((option) => urlVariableNames.includes(option)).concat("baseUrl"); | |
| const remainingParameters = omit(parameters, omittedParameters); | |
| const isBinaryRequest = /application\/octet-stream/i.test(headers.accept); | |
| if (!isBinaryRequest) { | |
| if (options.mediaType.format) { | |
| headers.accept = headers.accept.split(/,/).map( | |
| (format) => format.replace( | |
| /application\/vnd(\.\w+)(\.v3)?(\.\w+)?(\+json)?$/, | |
| `application/vnd$1$2.${options.mediaType.format}` | |
| ) | |
| ).join(","); | |
| } | |
| if (url.endsWith("/graphql")) { | |
| if (options.mediaType.previews?.length) { | |
| const previewsFromAcceptHeader = headers.accept.match(/[\w-]+(?=-preview)/g) || []; | |
| headers.accept = previewsFromAcceptHeader.concat(options.mediaType.previews).map((preview) => { | |
| const format = options.mediaType.format ? `.${options.mediaType.format}` : "+json"; | |
| return `application/vnd.github.${preview}-preview${format}`; | |
| }).join(","); | |
| } | |
| } | |
| } | |
| if (["GET", "HEAD"].includes(method)) { | |
| url = addQueryParameters(url, remainingParameters); | |
| } else { | |
| if ("data" in remainingParameters) { | |
| body = remainingParameters.data; | |
| } else { | |
| if (Object.keys(remainingParameters).length) { | |
| body = remainingParameters; | |
| } | |
| } | |
| } | |
| if (!headers["content-type"] && typeof body !== "undefined") { | |
| headers["content-type"] = "application/json; charset=utf-8"; | |
| } | |
| if (["PATCH", "PUT"].includes(method) && typeof body === "undefined") { | |
| body = ""; | |
| } | |
| return Object.assign( | |
| { method, url, headers }, | |
| typeof body !== "undefined" ? { body } : null, | |
| options.request ? { request: options.request } : null | |
| ); | |
| } | |
| export { | |
| parse | |
| }; | |