Skip to content

Commit

Permalink
Introduce the new typescript client package (#434)
Browse files Browse the repository at this point in the history
* chore(poc): add the poc directory

- add the poc folder and all the associated files and directories
- initial commit of the entire work so far

* feat(auth-middleware): implement auth middleware

- implement anonymous-auth-flow
- refactor auth request builder
- refactor auth request executor

* feat(auth-middleware): auth middleware flows

- add existing-auth-flow
- add refresh-token-auth-flow
- minor refactor

* feat(password-auth-flow): password authentication flow

- implement password authentication flow

* feat(auth-middleware): implement token cache

- implement refresh token and token cache functionality
- refactor executor function

* feat(http-client): add support for different http client

- add support for node-fetch
- add support for axios
- add some refactoring

* feat(middleware): concurrent modification middleware

- add middleware to handle concurrent modification errors
- add minor refactoring

* chore(sdk-client): restructure to include client and tests

- restructure in sdk-client-v3 folder

* feat(abort-controller): add abort-controller

- add abort-controller and request abort timeout
- add withRetryMiddleware method in the client builder class
- remove poc folder
- minor refactor

* chore(refactor): refactor and improve types

- refactor and improve typings for tokenCache
- adapt tokenCache store to also be used for requestSate store

* chore(refactor): refactor

- add projecKey, scope and httpClient options to defaultClient method
- add tokenCacheOptions to the set method of the store function

* chore(enableRetry): remove enableRetry option

- remove enableRetry option from create-retry-middleware function
- remove property from the RetryMiddlewareOptions type

* chore(error-middleware-n-axios): error-middleware and axios

- complete error middleware features and functionalities
- refactor auth middleware request executor to support axios
- minor bug fixes

* chore(unit-test): add unit tests

* chore(completed-unit-test): completed unit test for PoC

- include tests for all middlewares

* chore(beta-release): prepare beta release

* chore(axios): remove axios dependency

- remove axios dependency
- remove script.sh script
- remove empty index file in test file
- refactor

* chore(changeset): add changeset

* chore(changeset): remove redundant changeset

- remove changeset since a PR won't be created for this change

* v0.0.0-beta.3

* chore(types.d.ts): undo type optimization

* chore(types.d.ts): improve types

* v0.0.0-beta.4

* chore(ts-client): improve types and refactor

* v0.0.0-beta.5

* v0.0.0-beta.6

* chore(client): restructure response to conform with v2

- add code to error response
- add body property for error response
- add appropriate types to modified response

* v0.0.0-beta.7

* fix(ts-client): restructure client response to include statusCode

* v0.0.0-beta.8

* feat(retry-middleware): change the current implementation of the retry-middleware

- move the retry functionality to the http-middleware
- refactor types.d.ts to conform with new implementation
- refactor http-middleware to include error message and stack traces

* v0.0.0-beta.10

* chore(errors): improve error response

- address breaking changes in error response
- improve the executor function for both fetch and axios
- change fetch deserializer from json to text
- fix error with uri when originalRequest is excluded from respose
- include minor code refactor

* chore(ts-client): update @commercetools/sdk-client-v3 to version 0.0.0-beta.11

* fix(create-http-middleware): unset null content-type header

- unset null content-type header to avoid 415 error when uploading images using the SDK

* chore(test): add more tests

- add more tests to improve coverage

* chore(abort-controller): add abort-controller

- add abort-controller as a dev-dependency

* chore(test): add more tests

- add more tests to improve coverage

* chore(dependencies): update and clean dependencies

* chore(dependencies): update dependencies

* Update packages/sdk-client-v3/LICENSE

Co-authored-by: Jens Schulze <[email protected]>

* chore(sdk-client-v3): optionally chain the version number

- optionally chain the current version in the concurrentModification middleware

* chore(yarn.lock): fix conflicts

- fix merge conflicts in yarn.lock file

* chore(postbuild-script): correct postbuild script

- correct npm postbuild script

* chore(pr-feedback): implement feedback)

- implement team feedback

* chore(): implement feedback

- implement team feedback

* chore(feedback): implement feedback

- implement team feedback

* Create stupid-bears-report.md

Add release changeset

---------

Co-authored-by: Jens Schulze <[email protected]>
  • Loading branch information
ajimae and jenschude authored Apr 11, 2023
1 parent 7a7bb71 commit ea0a6c6
Show file tree
Hide file tree
Showing 66 changed files with 7,442 additions and 84 deletions.
22 changes: 22 additions & 0 deletions .changeset/stupid-bears-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@commercetools/ts-client": major
---

Introduce the new typescript client package with the following eatures and middleware
- [x] add client and middleware composer
- [x] add http-middleware
- [x] add error-middleware
- [x] add logger-middleware
- [x] add auth-middleware
- [x] add with-client-credentials-flow
- [x] add with-anonymous-session-flow
- [x] add with-password-flow
- [x] add with-refresh-token-flow
- [x] add with-existing-token-flow
- [x] add token cache
- [x] add retry-middleware
- [x] add correlation-id-middleware
- [x] add queue-middleware
- [x] add user-agent-middleware
- [x] add concurrent-modification-middleware
- [x] add axios and node-fetch support
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ training-tmp/
loadtest/
.npmignore
api.http
vue
vue
poc
21 changes: 21 additions & 0 deletions packages/sdk-client-v3/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 commercetools

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
126 changes: 126 additions & 0 deletions packages/sdk-client-v3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Commercetools Composable Commerce (Improved) TypeScript SDK client (beta)

This is the new and improved Typescript SDK client.

## Usage examples

```bash
npm install --save @commercetools/ts-client
npm install --save @commercetools/platform-sdk

or

yarn add @commercetools/ts-client
yarn add @commercetools/platform-sdk
```

```ts
import {
type Next,
type HttpMiddlewareOptions,
type AuthMiddlewareBaseOptions,
type ClientRequest,
type MiddlewareRequest,
type MiddlewareResponse,
type Client,
ClientBuilder,
} from '@commercetools/ts-client'
import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'
import fetch from 'node-fetch'

const projectKey = 'mc-project-key'
const authMiddlewareOptions = {
host: 'https://auth.europe-west1.gcp.commercetools.com',
projectKey,
credentials: {
clientId: 'mc-client-id',
clientSecret: 'mc-client-secrets',
},
oauthUri: '/oauth/token', // - optional: custom oauthUri
scopes: [`manage_project:${projectKey}`],
httpClient: fetch,
}

const httpMiddlewareOptions = {
host: 'https://api.europe-west1.gcp.commercetools.com',
httpClient: fetch,
}

const retryOptions = {
maxRetries: 3,
retryDelay: 200,
backoff: true,
retryCodes: [200],
}

// custom middleware
function middleware(options) {
return (next: Next) =>
async (request: MiddlewareRequest): Promise<MiddlewareResponse> => {
const { response, ...rest } = request

// other actions can also be carried out here e.g logging,
// error handling, injecting custom headers to http requests etc.
console.log({ response, rest })
return next({ ...request })
}
}

const client: Client = new ClientBuilder()
.withPasswordFlow(authMiddlewareOptions)
.withLoggerMiddleware({
includeOriginalRequest: false,
includeResponseHeaders: false,
})
.withCorrelationIdMiddleware({
generate: () => 'fake-correlation-id' + Math.floor(Math.random() + 2),
})
.withHttpMiddleware(httpMiddlewareOptions)
.withRetryMiddleware(retryOptions)
.withMiddleware(middleware({})) // <<<------------------- add the custom middleware here
.withErrorMiddleware({})
.build()

const apiRoot = createApiBuilderFromCtpClient(client)

// calling the Composable Commerce `api` functions
// get project details
apiRoot
.withProjectKey({ projectKey })
.get()
.execute()
.then((x) => {
/*...*/
})

// create a productType
apiRoot
.withProjectKey({ projectKey })
.productTypes()
.post({
body: { name: 'product-type-name', description: 'some description' },
})
.execute()
.then((x) => {
/*...*/
})

// create a product
apiRoot
.withProjectKey({ projectKey })
.products()
.post({
body: {
name: { en: 'our-great-product-name' },
productType: {
typeId: 'product-type',
id: 'some-product-type-id',
},
slug: { en: 'some-slug' },
},
})
.execute()
.then((x) => {
/*...*/
})
```
61 changes: 61 additions & 0 deletions packages/sdk-client-v3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@commercetools/ts-client",
"version": "0.0.0-beta.11",
"engines": {
"node": ">=14"
},
"description": "commercetools Composable Commerce TypeScript SDK client.",
"keywords": [
"commercetools",
"composable commerce",
"sdk",
"typescript",
"client",
"middleware",
"http",
"oauth",
"auth"
],
"homepage": "https://github.com/commercetools/commercetools-sdk-typescript",
"license": "MIT",
"directories": {
"lib": "lib",
"test": "test"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/commercetools/commercetools-sdk-typescript.git"
},
"bugs": {
"url": "https://github.com/commercetools/commercetools-sdk-typescript/issues"
},
"dependencies": {
"abort-controller": "3.0.0",
"buffer": "^6.0.3",
"node-fetch": "^2.6.1",
"querystring": "^0.2.1"
},
"files": ["dist", "CHANGELOG.md"],
"author": "Chukwuemeka Ajima <[email protected]>",
"main": "dist/commercetools-ts-client.cjs.js",
"module": "dist/commercetools-ts-client.esm.js",
"browser": {
"./dist/commercetools-ts-client.cjs.js": "./dist/commercetools-ts-client.browser.cjs.js",
"./dist/commercetools-ts-client.esm.js": "./dist/commercetools-ts-client.browser.esm.js"
},
"devDependencies": {
"common-tags": "1.8.2",
"dotenv": "16.0.3",
"jest": "29.5.0",
"nock": "12.0.3",
"organize-imports-cli": "0.10.0"
},
"scripts": {
"organize_imports": "find src -type f -name '*.ts' | xargs organize-imports-cli",
"postbuild": "yarn organize_imports",
"post_process_generate": "yarn organize_imports"
}
}
Loading

0 comments on commit ea0a6c6

Please sign in to comment.