Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
/ abstract-api-client Public archive

Abstract API Typescript client interfaces and SDK

License

Notifications You must be signed in to change notification settings

roadiz/abstract-api-client

Repository files navigation

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