Created
August 27, 2019 12:55
-
-
Save svaj/0d720d6658cdca7c2ce27ba7b9da71a6 to your computer and use it in GitHub Desktop.
ct with https proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createClient } from '@commercetools/sdk-client'; | |
import { createCorrelationIdMiddleware } from '@commercetools/sdk-middleware-correlation-id'; | |
import { createAuthMiddlewareForClientCredentialsFlow } from '@commercetools/sdk-middleware-auth'; | |
import { createHttpMiddleware } from '@commercetools/sdk-middleware-http'; | |
import { createQueueMiddleware } from '@commercetools/sdk-middleware-queue'; | |
import { createRequestBuilder } from '@commercetools/api-request-builder'; | |
import { createUserAgentMiddleware } from '@commercetools/sdk-middleware-user-agent'; | |
import uuid from 'uuid/v4'; | |
import fetch from 'node-fetch'; | |
import HttpsProxyAgent from 'https-proxy-agent'; | |
export const fetchPatched = (url, options = {}) => { | |
const instanceOptions = { | |
...options, | |
}; | |
if (!options.agent && process.env.HTTP_PROXY) { | |
instanceOptions.agent = new HttpsProxyAgent(process.env.HTTP_PROXY); | |
} | |
return fetch(url, instanceOptions); | |
}; | |
const userAgentMiddleware = createUserAgentMiddleware(userAgentOptions); | |
const credentials = { | |
clientId, | |
clientSecret, | |
}; | |
const authOptions = { | |
host: oauthHost, | |
projectKey, | |
credentials, | |
fetch: fetchPatched, // Use fetch with proxy | |
}; | |
const httpOptions = { | |
includeOriginalRequest: true, | |
includeResponseHeaders: true, | |
host, | |
fetch: fetchPatched, | |
enableRetry: true, | |
}; | |
const middlewares = [ | |
createAuthMiddlewareForClientCredentialsFlow(authOptions), | |
userAgentMiddleware, | |
createCorrelationIdMiddleware({ | |
generate: () => `offers/${uuid()}`, | |
}), | |
createQueueMiddleware({ concurrency }), | |
createHttpMiddleware(httpOptions), | |
]; | |
commercetools.client = createClient({ | |
middlewares, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment