spree-api-v2-js-sdk icon indicating copy to clipboard operation
spree-api-v2-js-sdk copied to clipboard

feat: implement fluent interface

Open fpdrozd opened this issue 2 years ago • 0 comments

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 avatar Dec 22 '22 13:12 fpdrozd