@wearejh/swagger-rxjs

> TODO: description

Usage no npm install needed!

<script type="module">
  import wearejhSwaggerRxjs from 'https://cdn.skypack.dev/@wearejh/swagger-rxjs';
</script>

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:

  • Type safety on request payload
  • Type safety on path parameters
  • Type safety support for 1 level of 'fields'

Example GET

All following examples assume a global setup such as
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/CustomerCustomerRepositoryV1GetByIdGet2";

// 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/IntegrationCustomerTokenServiceV1CreateCustomerAccessTokenPost";

// Typescript will enforce the correct body params are given here
execute({username: "shane@example.com", password: "123456"}, commonDeps)
    .subscribe(token => console.log(`Token: ${token}`))