spree-api-v2-js-sdk
spree-api-v2-js-sdk copied to clipboard
feat: implement fluent interface
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:
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
):

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