@wearejh/swagger-rxjs

0.23.2 • Public • Published

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

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/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: "shane@example.com", password: "123456"}, commonDeps)
    .subscribe(token => console.log(`Token: ${token}`))

Readme

Keywords

none

Package Sidebar

Install

npm i @wearejh/swagger-rxjs

Weekly Downloads

183

Version

0.23.2

License

ISC

Unpacked Size

1.17 MB

Total Files

863

Last publish

Collaborators

  • oliverchenery
  • jasonujmaalvis
  • yutonet
  • domjtalbot
  • jhdeploy