0.7.0 • Published 5 months ago

@content-island/api-client v0.7.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@content-island/api-client

Installation

npm install @content-island/api-client

Examples

Basic Usage

// Your model
interface Post {
  id: string;
  title: string;
  body: string;
  order: number;
  language: 'es' | 'en';
}
import { createClient, mapContentToModel } from '@content-island/api-client';

const client = createClient({ accessToken: <your-token>});

const postContents = client.getContentList({ contentType: 'post'}); // Retrieve the list of contents in the project filtered by content type, for example 'post'
const englishPosts = postContents.map(content => mapContentToModel<Post>(content, 'en')); // Map the english content to your own model
const spanishPosts = postContents.map(content => mapContentToModel<Post>(content, 'es')); // Map the spanish content to your own model

// Or you can retrieve a content by id
const content = client.getContent('content-id'); // Retrieve a content by id
const englishPost = mapContentToModel<Post>(content, 'en'); // Map the english content to your own model
const spanishPost = mapContentToModel<Post>(content, 'es'); // Map the spanish content to your own model

Filter by language

import { createClient, mapContentToModel } from '@content-island/api-client';

const client = createClient({ accessToken: <your-token>});

const englishPostContents = client.getContentList({ contentType: 'post', language: 'en'}); // Retrieve the english posts
const englishPosts = englishPostContents.map(content => mapContentToModel<Post>(content)); // Map content to your own model using the first language available, in this case 'en'

// Or you can retrieve contents by multiple languages
const postContents = client.getContentList({ contentType: 'post', language: { in: ['en', 'es']}}); // Retrieve the english or spanish posts

// Or you can retrieve a content by id
const englishContent = client.getContent('content-id', { language: 'en'}); // Retrieve a content by id
const englishPost = mapContentToModel<Post>(englishContent); // Map content to your own model using the first language available, in this case 'en'

Check the API/queryParams section for more information about the query parameters.

API

createClient(options): endpoints

Creates a client instance.

options

nametyperequireddescription
accessTokenstringtrueThe access token of the project
domainstringfalseThe domain of the project. Default: api.contentisland.net
apiVersionstringfalseThe version of the API. Default: 1.0

endpoints

getProject()

Retrieves the project details by access token.

getContentList(queryParams?)

Retrieves the list of contents in the project.

getContent(id, queryParams?)

Retrieves a content by id.

queryParams

The query parameters to filter the list of contents.

KeyValueDescription
idFilterThe id of the content to retrieve. This is useful to retrieve a list of contents by id. For example: client.getContentList({ id: { in: ['1', '2', '3']})
contentTypeFilterThe content type to filter the list of contents. For example: post
languageFilterThe language to filter the list of contents. For example: en
Filter typeDescriptionExample
stringFilter the content where the param is equal to this value{ contentType: 'post' }
{ in: string[] }Filter the content where the param contains some of these values{ language: { in: ['en', 'es'] } }

mapContentToModel(content, language?)

Maps a list of fields to a model.

const content: Content = {
  id: '1',
  contentType: { id: '10', name: 'post' },
  lastUpdate: '2023-10-20T00:00:00.000Z',
  fields: [
    {
      id: '100',
      name: 'title',
      value: 'My title',
      type: 'short-text',
      isArray: false,
      language: 'en',
    },
    {
      id: '200',
      name: 'body',
      value: '# My long text in markdown',
      type: 'long-text',
      isArray: false,
      language: 'en',
    },
    {
      id: '300',
      name: 'order',
      value: 1,
      type: 'number',
      isArray: false,
      language: 'en',
    },
    {
      id: '400',
      name: 'title',
      value: 'Mi titutlo',
      type: 'short-text',
      isArray: false,
      language: 'es',
    },
    {
      id: '500',
      name: 'body',
      value: '# Mi texto largo en markdown',
      type: 'long-text',
      isArray: false,
      language: 'es',
    },
    {
      id: '600',
      name: 'order',
      value: 1,
      type: 'number',
      isArray: false,
      language: 'es',
    },
  ],
};

interface Post {
  id: string;
  title: string;
  body: string;
  order: number;
}

const post = mapContentToModel<Post>(fields); // Get the first language available, in this case 'en'
console.log(post); // { id: '1', title: 'My title', body: '# My long text in markdown', order: 1 }

const englishPost = mapContentToModel<Post>(fields, 'en');
console.log(englishPost); // { id: '1', title: 'My title', body: '# My long text in markdown', order: 1 }

const spanishPost = mapContentToModel<Post>(fields, 'es');
console.log(spanishPost); // { id: '1', title: 'Mi titulo', body: '# Mi texto largo en markdown', order: 1 }
0.5.0

8 months ago

0.7.0

5 months ago

0.6.0

7 months ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago