Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement fluent interface #361

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

feat: implement fluent interface #361

wants to merge 15 commits into from

Conversation

fpdrozd
Copy link
Contributor

@fpdrozd fpdrozd commented Dec 22, 2022

This PR adds the fluent interface implementation proposed here.

The Client class has been moved from the core package to the corresponding SDK packages because of typescript's problems with inferring the endpoint types when they were being appended dynamically (they have to be specifically bound to this.).

All the types responsible for fluent interface functionality will stay in the sdk-core, this way we can go ahead and simply implement it in the Platform SDK.

WithClientBuilderOptions type

Now the type supports passing a union of required props names which results in checking if at least one of them is satisfied. This behavior is meant for the endpoints that require either bearer_token or order_token.

Example:

type ClientOptions = {
  bearer_token: false
  order_token: false
  locale: false
  currency: false
}

type options = WithClientBuilderOptions<
  ClientOptions,
  'bearer_token' | 'order_token',
  {
    user: {
      email: string
    }
  }
>

const endpointOptions: options = {
  order_token: 'order-token',
  user: {
    email: '[email protected]'
  }
}

Examples

Because of the feature's backwards compatibility the SDK can be used as before:

Screenshot 2022-12-22 at 14 12 07

With the new fluent interface approach (since bearer_token is the only required option accountInfo options can be skipped completely when provided with the token by withBearerToken):

Screenshot 2022-12-22 at 14 15 58

Otherwise, if the token is not provided, we get a compilation error:

Screenshot 2022-12-22 at 14 16 48

@fpdrozd fpdrozd marked this pull request as ready for review December 22, 2022 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants