@wearejh/swagger-rxjs
v0.31.0
Published
> TODO: description
Downloads
1,025
Keywords
Readme
mage-swagger-rxjs
auto-generated REST API calls for Magento 2
This library takes the Swagger json
file produced by Magento and turns
it into individual, strongly typed API calls.
Features:
- [x] Type safety on request payload
- [x] Type safety on path parameters
- [x] Type safety support for 1 level of 'fields'
Example GET
import { ajax } from "rxjs/ajax";
// These would be setup & used site-wide (omitted from following examples)
const commonDeps = {
apiUrl: (operationId) => (path, params, fields) => path,
restHeaders: () => {
const token = store.getState().user.token; // or any way of getting the token
return {
Authorization: token ? `Bearer ${token}` : '',
}
},
getJSON: (path, headers) => ajax.getJSON(path, headers),
}
To get access the path /V1/customers/me
import { execute } from "mage-swagger-rxjs/ts/GetV1CustomersMe";
// response is now `CustomerDataCustomerInterface`
execute(commonDeps).subscribe(customer => console.log(customer))
If you only want to access a subset of fields, you can provide an array of keys
// The response here will be narrowed to Pick<CustomerDataCustomerInterface, "addresses">
// which means Typescript knows the response will ONLY contain { "addresses": [] }
// Also all strings in this array are checked to ensure they exist on the target
execute(commonDeps, ["addresses"]).subscribe(customer => console.log(customer))
Example POST
To get an access token from /V1/integration/customer/token
import { execute } from "mage-swagger-rxjs/ts/PostV1IntegrationCustomerToken";
// Typescript will enforce the correct body params are given here
execute({username: "[email protected]", password: "123456"}, commonDeps)
.subscribe(token => console.log(`Token: ${token}`))