How to use @commercetools/sdk-middleware-http - 10 common examples

To help you get started, we’ve selected a few @commercetools/sdk-middleware-http examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github commercetools / merchant-center-application-kit / packages / sdk / src / test-utils / test-utils.spec.ts View on Github external
beforeEach(() => {
        const BadRequestError = getErrorByCode(400);
        const error = new BadRequestError('Invalid parameter');
        const testMiddleware = createTestMiddleware([
          // @ts-ignore
          { action: { type: 'another-action' } },
          {
            action: sdkActions.get({ uri: '/foo/1' }),
            error,
          },
          {
            action: sdkActions.post({
              uri: '/foo',
              payload: JSON.stringify({ foo: 'bar' }),
            }),
            response: { foo: 'bar' },
          },
        ]);
github commercetools / nodejs / packages / product-json-to-csv / src / main.js View on Github external
parserConfig: ParserConfigOptions,
    logger: LoggerOptions,
    accessToken: string
  ) {
    this.apiConfig = apiConfig
    this.client = createClient({
      middlewares: [
        createAuthMiddlewareForClientCredentialsFlow({
          ...this.apiConfig,
          fetch,
        }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: this.apiConfig.apiUrl,
          fetch,
        }),
      ],
    })

    const defaultConfig = {
      categoryBy: 'name', // key, externalId or namedPath supported
      categoryOrderHintBy: 'name', // key, externalId or name supported
      delimiter: ',',
      fillAllRows: false,
      onlyMasterVariants: false,
      language: 'en',
      languages: ['en'],
      multiValueDelimiter: ';',
      encoding: 'utf8',
github commercetools / nodejs / integration-tests / ts-sdk / ts-sdk.it.js View on Github external
describe('Anonymous Session Flow', () => {
    const httpMiddleware = createHttpMiddleware({
      host: 'https://api.sphere.io',
      fetch,
    })

    it('create an anonymous session and a cart tied to the session', () => {
      const authConfig = {
        ...apiConfig,
        ...{ scopes: [`manage_project:${projectKey}`] },
        ...{
          credentials: {
            clientId: apiConfig.credentials.clientId,
            clientSecret: apiConfig.credentials.clientSecret,
          },
        },
        fetch,
      }
github commercetools / nodejs / integration-tests / sdk / channels.it.js View on Github external
describe('Channels', () => {
  const ignoredResponseKeys = [
    'id',
    'createdAt',
    'createdBy',
    'lastModifiedAt',
    'lastModifiedBy',
  ]
  const service = createRequestBuilder({ projectKey }).channels
  const httpMiddleware = createHttpMiddleware({
    host: 'https://api.sphere.io',
    fetch,
  })
  const queueMiddleware = createQueueMiddleware({
    concurrency: 5,
  })
  const userAgentMiddleware = createUserAgentMiddleware({
    libraryName: pkg.name,
    libraryVersion: pkg.version,
    contactUrl: 'https://github.com/commercetools/nodejs',
    contactEmail: 'npmjs@commercetools.com',
  })
  const key = uniqueId('channel_')
  let channelResponse
  let client
  let apiConfig
github commercetools / nodejs / packages / product-exporter / src / main.js View on Github external
exportConfig: ExportConfigOptions,
    logger: LoggerOptions,
    accessToken: string
  ) {
    this.apiConfig = apiConfig
    this.client = createClient({
      middlewares: [
        createAuthMiddlewareForClientCredentialsFlow({
          ...this.apiConfig,
          fetch,
        }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: this.apiConfig.apiUrl,
          enableRetry: true,
          fetch,
        }),
      ],
    })

    const defaultConfig = { staged: false, json: true }

    this.exportConfig = { ...defaultConfig, ...exportConfig }
    this.logger = {
      error: () => {},
      warn: () => {},
      info: () => {},
      debug: () => {},
      ...logger,
github commercetools / nodejs / packages / discount-code-importer / src / main.js View on Github external
)
    this.apiConfig = options.apiConfig
    this.accessToken = options.accessToken
    this.batchSize = options.batchSize || 50
    this.continueOnProblems = options.continueOnProblems || false
    this.client = createClient({
      middlewares: [
        createAuthMiddlewareForClientCredentialsFlow({
          ...this.apiConfig,
          fetch,
        }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: this.apiConfig.apiUrl,
          fetch,
        }),
      ],
    })

    this.syncDiscountCodes = createSyncDiscountCodes()

    this.logger = logger || {
      error: npmlog.error.bind(this, ''),
      warn: npmlog.warn.bind(this, ''),
      info: npmlog.info.bind(this, ''),
      verbose: npmlog.verbose.bind(this, ''),
    }
    this._resetSummary()
  }
github commercetools / nodejs / packages / csv-parser-state / src / main.js View on Github external
apiConfig: ApiConfigOptions,
    accessToken: string
  ): Object {
    if (!apiConfig)
      throw new Error('The constructor must be passed an `apiConfig` object')
    return createClient({
      middlewares: [
        createAuthMiddlewareWithExistingToken(
          accessToken ? `Bearer ${accessToken}` : ''
        ),
        createAuthMiddlewareForClientCredentialsFlow({ ...apiConfig, fetch }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: apiConfig.apiUrl,
          fetch,
        }),
      ],
    })
  }
github commercetools / nodejs / packages / discount-code-exporter / src / main.js View on Github external
if (!options.apiConfig)
      throw new Error('The contructor must be passed an `apiConfig` object')
    if (options.batchSize > 500)
      throw new Error('The `batchSize` must not be more than 500')
    this.apiConfig = options.apiConfig
    this.client = createClient({
      middlewares: [
        createAuthMiddlewareForClientCredentialsFlow({
          ...this.apiConfig,
          fetch,
        }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: this.apiConfig.apiUrl,
          enableRetry: true,
          fetch,
        }),
      ],
    })

    const defaultOptions = {
      delimiter: ',',
      multiValueDelimiter: ';',
      batchSize: 500,
      exportFormat: 'json',
      language: 'en',
      fields: [],
    }
github commercetools / nodejs / packages / category-exporter / src / main.js View on Github external
throw new Error('The constructor must be passed an `apiConfig` object')
    this.apiConfig = options.apiConfig
    this.client = createClient({
      middlewares: [
        createAuthMiddlewareWithExistingToken(
          options.accessToken ? `Bearer ${options.accessToken}` : ''
        ),
        createAuthMiddlewareForClientCredentialsFlow({
          ...this.apiConfig,
          fetch,
        }),
        createUserAgentMiddleware({
          libraryName: pkg.name,
          libraryVersion: pkg.version,
        }),
        createHttpMiddleware({
          host: this.apiConfig.apiUrl,
          enableRetry: true,
          fetch,
        }),
      ],
    })

    this.predicate = options.predicate

    this.logger = {
      ...silentLogger,
      ...options.logger,
    }
  }
github commercetools / nodejs / packages / inventories-exporter / src / main.js View on Github external
this.logger = {
      error: () => {},
      info: () => {},
      warn: () => {},
      verbose: () => {},
      ...logger,
    }

    this.client = createClient({
      middlewares: [
        createAuthMiddlewareForClientCredentialsFlow({ ...apiConfig, fetch }),
        createUserAgentMiddleware({
          libraryName: name,
          libraryVersion: version,
        }),
        createHttpMiddleware({
          host: apiConfig.apiUrl,
          enableRetry: true,
          fetch,
        }),
      ],
    })
    this.exportConfig = exportConfig
    this.accessToken = accessToken
    this.reqBuilder = createRequestBuilder({
      projectKey: apiConfig.projectKey,
    })
  }

@commercetools/sdk-middleware-http

Middleware for http requests, to use with @commercetools/sdk-client

MIT
Latest version published 1 year ago

Package Health Score

70 / 100
Full package analysis