@roadiz/abstract-api-client
TypeScript icon, indicating that this package has built-in type declarations

3.1.4 • Public • Published

abstract-api-client

Abstract API Typescript client interfaces and SDK.

Based on Axios HTTP client.

Usage

Version 3.x should only be used with Roadiz v2.1+, for older versions use version 2.x

yarn add @roadiz/abstract-api-client

tsconfig.json

{
  "compilerOptions": {
    "types": [
      "@roadiz/abstract-api-client"
    ]
  }
}

Customize Roadiz API client against your own API schema

  • Download latest d.ts definition file from Roadiz backoffice
  • Extend RoadizApi class
export default class MyAwesomeRoadizApi extends RoadizApi {
    /*
     * Page node-type
     */
    getPages(params: RoadizRequestNSParams) {
        return this.get<HydraCollection<NSPage>, RoadizRequestNSParams>('pages', { params })
    }
    
    /*
     * BlogPost node-type
     */
    getBlogPosts(params: RoadizRequestNSParams) {
        // Additional default params…
        params = {
            order: {
                publishedAt: 'DESC'
            },
            ...params,
        }
        return this.get<HydraCollection<NSBlockPost>, RoadizRequestNSParams>('blog_posts', { params })
    }
}

Fetch all URLs for a sitemap

const api = new RoadizApi(process.env.API_BASE_URL,)

return api.fetchAllUrlsForLocale('fr').then((urls: Array<string>) => {
    // build your sitemap
})

Get all alternative URLs for a node-source

Alternative links are useful to build language navigation for each website page. It's based on HTTP response header Link. API getAlternateLinks method will return a Array<AlternateLink> from an AxiosResponse:

const api = new RoadizApi(process.env.API_BASE_URL)

api.getWebResponseByPath('/').then((response) => {
    /*
     * [{
     *     url: '/',
     *     locale: 'en'
     * }, {
     *     url: '/fr',
     *     locale: 'fr'
     * }]
     */
    api.getAlternateLinks(response)
})

Test

Tests use jest and require a working Roadiz headless API to fetch responses from. Copy .env.dist to .env and fill your server credentials.

yarn
cp .env.dist .env
yarn test

Readme

Keywords

none

Package Sidebar

Install

npm i @roadiz/abstract-api-client

Weekly Downloads

18

Version

3.1.4

License

MIT

Unpacked Size

31.8 kB

Total Files

13

Last publish

Collaborators

  • ambroisemaupate
  • rz_hakim
  • manuelodelain