|
| 1 | +const fetch = require('node-fetch').default || require('node-fetch') // Webpack will sometimes export default exports in different places |
| 2 | + |
| 3 | +const { getOperations } = require('../operations') |
| 4 | + |
| 5 | +const { getUrl } = require('./url.js') |
| 6 | +const { addBody } = require('./body.js') |
| 7 | +const { shouldRetry, waitForRetry, MAX_RETRY } = require('./retry.js') |
| 8 | +const { parseResponse, getFetchError } = require('./response.js') |
| 9 | + |
| 10 | +// For each OpenAPI operation, add a corresponding method. |
| 11 | +// The `operationId` is the method name. |
| 12 | +const addMethods = function(NetlifyApi) { |
| 13 | + const methods = getMethods(NetlifyApi) |
| 14 | + Object.assign(NetlifyApi, methods) |
| 15 | +} |
| 16 | + |
| 17 | +const getMethods = function(NetlifyApi) { |
| 18 | + const operations = getOperations() |
| 19 | + const methods = operations.map(method => getMethod(method, NetlifyApi)) |
| 20 | + return Object.assign({}, ...methods) |
| 21 | +} |
| 22 | + |
| 23 | +const getMethod = function(method, NetlifyApi) { |
| 24 | + return { |
| 25 | + [method.operationId](params, opts) { |
| 26 | + return callMethod(method, NetlifyApi, params, opts) |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const callMethod = async function(method, NetlifyApi, params, opts) { |
| 32 | + const requestParams = Object.assign({}, NetlifyApi.globalParams, params) |
| 33 | + const url = getUrl(method, NetlifyApi, requestParams) |
| 34 | + const optsA = getOpts(method, NetlifyApi, requestParams, opts) |
| 35 | + |
| 36 | + const response = await makeRequestOrRetry(url, optsA) |
| 37 | + |
| 38 | + const parsedResponse = await parseResponse(response) |
| 39 | + return parsedResponse |
| 40 | +} |
| 41 | + |
| 42 | +const getOpts = function({ verb, parameters }, NetlifyApi, { body }, opts) { |
| 43 | + const optsA = addHttpMethod(verb, opts) |
| 44 | + const optsB = addDefaultHeaders(NetlifyApi, optsA) |
| 45 | + const optsC = addBody(body, parameters, optsB) |
| 46 | + return optsC |
| 47 | +} |
| 48 | + |
| 49 | +// Add the HTTP method based on the OpenAPI definition |
| 50 | +const addHttpMethod = function(verb, opts) { |
| 51 | + return Object.assign({}, opts, { method: verb.toUpperCase() }) |
| 52 | +} |
| 53 | + |
| 54 | +// Assign default HTTP headers |
| 55 | +const addDefaultHeaders = function(NetlifyApi, opts) { |
| 56 | + return Object.assign({}, opts, { |
| 57 | + headers: Object.assign({}, NetlifyApi.defaultHeaders, opts.headers) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +const makeRequestOrRetry = async function(url, opts) { |
| 62 | + for (let index = 0; index <= MAX_RETRY; index++) { |
| 63 | + const response = await makeRequest(url, opts) |
| 64 | + |
| 65 | + if (shouldRetry(response, index)) { |
| 66 | + await waitForRetry(response) |
| 67 | + } else { |
| 68 | + return response |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const makeRequest = async function(url, opts) { |
| 74 | + try { |
| 75 | + return await fetch(url, opts) |
| 76 | + } catch (error) { |
| 77 | + throw getFetchError(error, url, opts) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = { addMethods } |
0 commit comments