commercetools-sdk-repositories
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

commercetools-sdk-repositories

commercetools-sdk-repositories This library contains repositories to work with commercetools-sdk-typescript

Install

npm i commercetools-sdk-repositories --save

Dependencies

This package requires @commercetools/sdk-client-v2 and @commercetools/platform-sdk

npm i @commercetools/sdk-client-v2 @commercetools/platform-sdk --save

Usage

Create a byProjectKeyRequest builder

JavaScript

const {
  AuthMiddlewareOptions,
  Client,
  ClientBuilder,
  HttpMiddlewareOptions,
} = require("@commercetools/sdk-client-v2");
const fetch = require("isomorphic-fetch");

const {
  ApiRoot,
  createApiBuilderFromCtpClient,
} = require("@commercetools/platform-sdk");
const { ByProjectKeyRequestBuilder } = require("@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder");

const auth = {
  host: "https://auth.commercetools.co",
  projectKey: "projectKey",
  credentials: {
    clientId: "clientId",
    clientSecret: "clientSecret"
  }
};
const http = {
  host: "https://api.commercetools.co",
  enableRetry: true,
  retryConfig: {
    maxRetries: 3
  },
};
const queue =  { concurrency: 10 };
const projectKey = "projectKey";

const httpMiddlewareOptions: HttpMiddlewareOptions = {
  ...http,
  fetch: http.fetch || fetch,
};

const clientBuilder: ClientBuilder = new ClientBuilder()
  .withProjectKey(projectKey)
  .withHttpMiddleware(httpMiddlewareOptions)
  .withLoggerMiddleware();

if (auth) {
  const authMiddlewareOptions: AuthMiddlewareOptions = {
    ...auth,
    fetch: auth.fetch || fetch,
  };
  clientBuilder.withClientCredentialsFlow(authMiddlewareOptions);
}

if (middleware) {
  clientBuilder.withMiddleware(middleware);
}

if (queue) {
  clientBuilder.withQueueMiddleware(queue);
}

const ctpClient: Client = clientBuilder.build();

const apiRoot: ApiRoot = createApiBuilderFromCtpClient(ctpClient);
const requestBuilder: ByProjectKeyRequestBuilder = apiRoot.withProjectKey({
  projectKey,
});

TypeScript

import {
  AuthMiddlewareOptions,
  Client,
  ClientBuilder,
  HttpMiddlewareOptions,
} from "@commercetools/sdk-client-v2";
import fetch from "isomorphic-fetch";

import {
  ApiRoot,
  createApiBuilderFromCtpClient,
} from "@commercetools/platform-sdk";
import { ByProjectKeyRequestBuilder } from "@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder";

const auth = {
  host: "https://auth.commercetools.co",
  projectKey: "projectKey",
  credentials: {
    clientId: "clientId",
    clientSecret: "clientSecret"
  }
};
const http = {
  host: "https://api.commercetools.co",
  enableRetry: true,
  retryConfig: {
    maxRetries: 3
  },
};
const queue =  { concurrency: 10 };
const projectKey = "projectKey";

const httpMiddlewareOptions: HttpMiddlewareOptions = {
  ...http,
  fetch: http.fetch || fetch,
};

const clientBuilder: ClientBuilder = new ClientBuilder()
  .withProjectKey(projectKey)
  .withHttpMiddleware(httpMiddlewareOptions)
  .withLoggerMiddleware();

if (auth) {
  const authMiddlewareOptions: AuthMiddlewareOptions = {
    ...auth,
    fetch: auth.fetch || fetch,
  };
  clientBuilder.withClientCredentialsFlow(authMiddlewareOptions);
}

if (middleware) {
  clientBuilder.withMiddleware(middleware);
}

if (queue) {
  clientBuilder.withQueueMiddleware(queue);
}

const ctpClient: Client = clientBuilder.build();

const apiRoot: ApiRoot = createApiBuilderFromCtpClient(ctpClient);
const byProjectKeyRequestBuilder: ByProjectKeyRequestBuilder = apiRoot.withProjectKey({
  projectKey,
});

Import and instantiate the repository you want to use

JavaScript

const { CartsRepository } = require("commercetools-sdk-client-repositories");

const cartsRepository = new CartsRepository(byProjectKeyRequestBuilder);

TypeScript

import { CartsRepository } from "commercetools-sdk-client-repositories";

const cartsRepository = new CartsRepository(byProjectKeyRequestBuilder);

Work with it

JavaScript

const cart = await cartsRepository.create({ currency: 'USD' });

TypeScript

const cart = await cartsRepository.create({ currency: 'USD' });

Dependents (0)

Package Sidebar

Install

npm i commercetools-sdk-repositories

Weekly Downloads

365

Version

1.0.3

License

ISC

Unpacked Size

365 kB

Total Files

155

Last publish

Collaborators

  • fmoro