10.22.2 • Published 12 days ago

fsxa-api v10.22.2

Weekly downloads
63
License
Apache-2.0
Repository
github
Last release
12 days ago

JavaScript Content API Library

The JavaScript Content API Library a.k.a. Content API is an interface handling data coming from the FirstSpirit CaaS and the Navigation Service. The data is processed and transformed so that it can be used in any JavaScript project.

Attention We would like to inform you that the project previously known as "FSXA-API" has recently undergone a name change. The project is now called "JavaScript Content API Library", but please note that this change solely affects the project's name, repository, and associated branding elements. The core code and functionality of the library remain unchanged. The purpose and scope of the library, as well as its intended usage and compatibility, remain consistent. The decision to change the name was made as the term "fsxa" no longer accurately represents the project's current scope, as other components beyond the fsxa-api are now discontinued. The name change aims to align the project's branding with its refined purpose and to avoid any confusion regarding its functionality.

Experimental features

Features marked as experimental are subject to change as long as they remain in the experimental state. Breaking changes to experimental features are not reflected in a major version changes.

Legal Notices

JavaScript Content API Library is a product of Crownpeak Technology GmbH, Dortmund, Germany. The JavaScript Content API Library is subject to the Apache-2.0 license.

Methods

In this section all available methods will be explained using examples.

Requirements

The JavaScript Content API Library requires a fetch implementation to run on a Node server environment. This dependency can be fulfilled with a cross-fetch polyfill in older Node versions. A working example can be found in the chapter Constructor.

Constructor

To be able to use the Content API, a new object must be created. How you create the object depends on how you want to use the Content API.

If you want to use the information provided by the CaaS in your frontend, you can use the FSXAProxyApi. It proxies the requested resources to a middleware Rest Api, that does not expose secrets. If you want to use it in your server, you can use the FSXARemoteApi. It can be registered as a Rest Api that can be called from a FSXAProxyApi instance.

However, to have a fully running application, we recommend using the Content API in your server as well as in your frontend.

The config mode can be preview or release, depending on the state of the content you want to fetch. There is an enum to use these modes. FSXAContentMode.PREVIEW for preview FSXAContentMode.RELEASE for release

If you want to use the FSXARemoteApi, you have to specify the following parameters:

const config = {
  apikey: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'!,
  caasURL: 'https://your.caas.url',
  contentMode: FSXAContentMode.PREVIEW,
  navigationServiceURL: 'https://your.navigation-service.url/navigation',
  projectID: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
  tenantID: 'your-tenant-id',
  logLevel: LogLevel.INFO,
  enableEventStream: true || false,
}

You can also include remote projects if you want to use remote media.

Attention Currently the Content API can only work with the master language of the remote media project. You also require a configured CAAS API key with read permissions to both projects.

For this you can add another parameter called remotes to the config. This parameter expects an object, which requires a unique name as key and an object as value. This object must have two keys. On the one hand an id with the project id as the value and on the other the locale with the locale abbreviation. For example:

const config = {
  ...
  remotes: { media: { id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', locale: 'en_GB' } },
  ...
}

The log level can be: 0 = Info 1 = Log 2 = Warning 3 = Error 4 = None. The default is set to 3.

Here is an example of how the FSXARemoteApi and FSXAProxyApi could be used with an Express.js backend. Make sure you have cross-fetch, express, cors, lodash and of course fsxa-api installed.

import dotenv from 'dotenv'
import express from 'express'
import cors from 'cors'
import {
  FSXAContentMode,
  LogLevel,
  FSXARemoteApi,
} from 'fsxa-api'
import { FSXAProxyApi } from 'fsxa-proxy-api'
import getExpressRouter from 'fsxa-api/dist/lib/integrations/express'
import 'cross-fetch/polyfill'

dotenv.config({ path: '.env' })
const app = express()
const remoteApi = new FSXARemoteApi(config)

app.use(cors())
app.use('/api', getExpressRouter({ api: remoteApi }))

app.listen(3002, async () => {
  console.log('Listening at http://localhost:3002')
  try {
    const locale = 'de_DE'
    const proxyAPI = new FSXAProxyApi(
      'http://localhost:3002/api',
      LogLevel.INFO
    )
    // you can also fetch navigation from proxyAPI
    const navigationResponse = await proxyAPI.fetchNavigation({
      locale,
      initialPath: '/',
    })
  } catch (e) {
    console.log(e)
  }
})

get authorizationHeader

Returns the build authorization header in the following format when using FSXARemoteApi:

{
  authorization: 'Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

Example:

const remoteApi = new FSXARemoteApi(config)
const authorizationHeader = remoteApi.authorizationHeader
// console.log { authorization: `Bearer ${config.apikey}` }

fetchNavigation

This method fetches the navigation from the configured navigation service. You need to pass a FetchNavigationParams object.

Example:

fsxaApi.fetchNavigation({
  locale: 'en_EN',
  initialPath: '/',
})

fetchElement

This method fetches an element from the configured CaaS. The FetchElementParams object defines options to specify your request. Check FSXARemoteApi.buildCaaSUrl to know which URL will be used.

fsxaApi.fetchElement({
  id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
  locale: 'de_DE',
})

Note The 'raw' CaaS data might be helpful when you want to know the key names that you can filter for. It is possible to access that data directly with the authorization header and the CaaS Url.

fetchByFilter

Returns the matching CaaS data entries. It maps the given entries to a more frontend-friendly format. Unnecessary fields will be omitted and the structure is simpler.

Expects as input parameter an array of filters and a language abbreviation. Optionally a page number, page size, sort and additional parameters can be passed. Optionally, a remoteProject can be passed. If one is passed, the element will be fetched from this project instead of the default one.

Several filter objects can be specified in the filter array, the results will then correspond to all specified filters.

One filter object must have a: field which specifies the searched key, operator which specifies the search operation, value which specifies the value that is looked for.

More information to the filters

In this example we search for all elements with the fsType equals Example. We want the 2nd page with a maximum of 50 entries. The results should be sorted by fsType descending. However, we do not want the identifier to appear:

fsxaApi.fetchByFilter({
  filters: [
    {
      field: 'fsType',
      operator: ComparisonQueryOperatorEnum.EQUALS,
      value: 'Example',
    },
  ],
  locale: 'en',
  page: 2,
  pagesize: 50,
  additionalParams: { keys: { identifier: 0 } },
  sort: [{ name: 'fsType', order: 'desc' }],
})

The default sorting is by the id descending. MultiSort is possible and the first sort param is prioritized over subsequent. The sorting is happening on the raw data.

Attention The keys names which are passed to the fetchByFilter method (e.g. in the filters or the additionalParams) have to match the key names that are present in the CaaS data.

fetchProjectProperties

Returns the project properties of the given language.

Expects a parameter object with the locale.

ATTENTION: Works only with CaaSConnect module version 3 onwards.

Example:

fsxaApi.fetchProjectProperties({
  locale: 'de_DE',
})

Filter

You can customize your queries in the fetchByFilter method with these operations. For more information please refer to the MongoDB documentation. Links are provided in each section.

Helpers

There are also some additional helper methods which can be used for different purposes.

type AvailableLocaleParams = {
  navigationServiceURL: string
  projectId: string
  contentMode: string | ('preview' | 'release')
}
getAvailableLocales({
  projectId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
  navigationServiceURL: 'https://your.navigation-service.url/navigation',
  contentMode: 'preview',
})

This method provides a list of available locales configured in your FirstSpirit project. It is recommended that the configuration matches the ISO format. E.g. "en_GB", "de_DE"

  Promise<string[]>

1. Extending the API

Introduction

In some scenarios, you might find that the standard methods provided by the JavaScript Content API Library do not fully cover your use case. That's where the flexibility of extending the API comes into play. This guide focuses on extending the API to fetch data from an aggregation in the Content as a Service (CaaS) using the buildCaasUrl function and handling authorization headers. We'll take you through the process step by step.

1.1 Fetching from Aggregation

This guide provided an in-depth overview of sending requests to the CaaS service using the buildCaasUrl function and the aggregation URI /_aggrs/example. It also covered the crucial aspect of including authorization headers to ensure secure communication. By following these steps, you can effectively retrieve and manage content from the CaaS in your JavaScript projects

1.1.1 Prerequisites

Before you start extending the API, ensure you have the following:

  • Configuration details such as API keys, CaaS URL, and other required parameters.

1.1.2 Abstract Steps

Extending the API to fetch data from an aggregation involves the following steps:

  1. Use the buildCaasUrl function to construct the URL for the aggregation request.
  2. Include the necessary authorization headers for secure communication.
  3. Make the fetch request to the constructed URL.
  4. Handle the response data or errors accordingly.

1.1.3 Implementation Notes

It's important to note a few things before proceeding:

  • This approach requires a server-side environment due to security concerns.
  • To access the CaaS service from the server-side, you need to use the FSXARemoteApi.
  • Extending the API in this manner allows you to tailor the data retrieval to your specific requirements.

1.1.4 How to Use the New Function

Let's dive into the details of extending the API to fetch data from an aggregation.

  1. Use buildCaasUrl Function

Start by using the buildCaasUrl function to construct the URL for the aggregation request:

import { FSXARemoteApi } from 'fsxa-api';

const remoteApi = new FSXARemoteApi(config);
const aggregationUri = '/_aggrs/example';
const caasUrl = remoteApi.buildCaasUrl();
const url = `${caasUrl}/${aggregationUri}`
  1. Include Authorization Headers

To ensure secure communication, include authorization headers in your request:

const headers = {
  ...remoteApi.authorizationHeader,
  // other headers if needed
}
  1. Make the Fetch Request
const caasUrl = remote.buildCaaSUrl();
const aggregationUri = '_aggrs/example';
const url = `${caasUrl}/${aggregationUri}`;
const headers = {
  ...remoteApi.authorizationHeader,
  // other headers if needed
}

try {
  const response = await fetch(url, {
    headers,
  });

  const data = await response.json();
  // Handle the data...
} catch (error) {
  console.error('An error occurred:', error);
  // Handle the error...
}

By following these steps, you've successfully extended the API to fetch data from an aggregation in the CaaS service. This approach empowers you to retrieve data tailored to your specific needs, enhancing the capabilities of the JavaScript Content API Library.

Logical Query Operators

These operators can also be found in the MongoDB Documentation

EnumOperation
LogicalQueryOperatorEnum.AND\$and
LogicalQueryOperatorEnum.NOT\$not
LogicalQueryOperatorEnum.NOR\$nor
LogicalQueryOperatorEnum.OR\$or

Comparison Query Operators

These operators can also be found in the MongoDB Documentation

EnumOperation
ComparisonQueryOperatorEnum.GREATER_THAN_EQUALS\$gte
ComparisonQueryOperatorEnum.GREATER_THAN\$gt
ComparisonQueryOperatorEnum.EQUALS\$eq
ComparisonQueryOperatorEnum.IN\$in
ComparisonQueryOperatorEnum.LESS_THAN\$lt
ComparisonQueryOperatorEnum.LESS_THAN_EQUALS\$lte
ComparisonQueryOperatorEnum.NOT_EQUALS\$ne
ComparisonQueryOperatorEnum.NOT_IN\$nin

Evaluation Query Operators

These operators can also be found in the MongoDB Documentation

EnumOperation
EvaluationQueryOperatorEnum.REGEX\$regex

Array Query Operators

These operators can also be found in the MongoDB Documentation

EnumOperation
ArrayQueryOperatorEnum.ALL\$all

Type Mapping

Input Components

This table gives an overview of the FirstSpirit input components, which can be defined in the "Form" tab of the FirstSpirit templates. Each input component has a (Java) data type, which has a representation in the CaaS. Those values are mapped to an interface of the Content API.

FirstSpirit Input ComponentCaaS RepresentationContent API Value
FS_CATALOGCatalog<Catalog$Card>= CaaSApi_FSCatalog= CaaSApi_Card[]Section[]
CMS_INPUT_CHECKBOXSet<Option>= CaaSApi_CMSInputCheckbox= CaaSApi_Option[]Option[]
CMS_INPUT_COMBOBOXOption= CaaSApi_CMSInputCombobox= CaaSApi_Option|nullOption
FS_DATASETDatasetContainer= CaaSApi_FSDataset= CaaSApi_Dataset|CaaSApi_DataEntry[]|nullDataset
CMS_INPUT_DATEDate= CaaSApi_CMSInputDate= string(ISO_8601)|nullDate
CMS_INPUT_DOMDomElement= CaaSApi_CMSInputDOM= string (FS-XML)RichTextElement[]
CMS_INPUT_DOMTABLETable= CaaSApi_CMSInputDOMTable= string (FS-XML)RichTextElement[]
CMS_INPUT_IMAGEMAPMappingMedium= CaaSApi_CMSImageMap= CaaSApi_CMSImageMapImageMap
FS_INDEXIndex<Index$Record>= CaaSApi_FSIndex= CaaSApi_Record[]DataEntries[]
CMS_INPUT_LINKLink= CaaSApi_CMSInputLink= ObjectLink
CMS_INPUT_LISTSet<Option>= CaaSApi_CMSInputList= any[]Option[]
CMS_INPUT_NUMBERNumber= CaaSApi_CMSInputNumber= numbernumber
CMS_INPUT_PERMISSIONPermissions= CaaSApi_CMSInputPermission= CaaSAPI_PermissionActivity[][]Permission
CMS_INPUT_RADIOBUTTONOption= CaaSApi_CMSInputRadioButton= CaaSApi_Option|nullOption
FS_REFERENCETargetReference= CaaSApi_FSReference= CaaSApi_BaseRef|CaaSApi_PageRefRef|CaaSApi_GCARef|CaaSApi_MediaRef|null
CMS_INPUT_TEXTString= CaaSApi_CMSInputText= stringstring
CMS_INPUT_TEXTAREAString= CaaSApi_CMSInputTextArea= stringstring
CMS_INPUT_TOGGLEBoolean= CaaSApi_CMSInputToggle= boolean|nullboolean

Disclaimer

This document is provided for information purposes only. Crownpeak Technology may change the contents hereof without notice. This document is not warranted to be error-free, nor subject to any other warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or fitness for a particular purpose. Crownpeak Technology specifically disclaims any liability with respect to this document and no contractual obligations are formed either directly or indirectly by this document. The technologies, functionality, services, and processes described herein are subject to change without notice.

10.22.2

12 days ago

10.22.1

1 month ago

10.22.0

1 month ago

10.21.0

2 months ago

10.20.0

2 months ago

10.19.1

5 months ago

10.19.0

6 months ago

10.18.2

7 months ago

10.18.1

7 months ago

10.16.3

8 months ago

10.17.0

8 months ago

10.18.0

7 months ago

10.16.2

9 months ago

10.16.1

10 months ago

10.15.1

11 months ago

10.15.0

11 months ago

10.16.0

11 months ago

10.12.0

1 year ago

10.13.1

11 months ago

10.13.0

11 months ago

10.14.0

11 months ago

10.14.1

11 months ago

10.11.3

1 year ago

10.11.2

1 year ago

10.11.1

1 year ago

10.11.0

1 year ago

10.10.2

1 year ago

10.10.3

1 year ago

10.10.0

1 year ago

10.10.1

1 year ago

10.8.0

1 year ago

10.8.1

1 year ago

10.9.0

1 year ago

10.7.0

1 year ago

10.6.3

2 years ago

10.6.4

2 years ago

10.6.5

2 years ago

10.6.6

2 years ago

10.6.7

2 years ago

10.6.0

2 years ago

10.6.1

2 years ago

10.6.2

2 years ago

10.5.1

2 years ago

10.0.0

2 years ago

10.2.0

2 years ago

10.2.1

2 years ago

10.4.0

2 years ago

10.5.0

2 years ago

10.3.2

2 years ago

10.1.0

2 years ago

10.1.1

2 years ago

10.3.0

2 years ago

10.3.1

2 years ago

9.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

8.4.0

2 years ago

8.3.0

2 years ago

8.3.1

2 years ago

6.1.0

2 years ago

7.1.0

2 years ago

6.0.0-alpha.2

2 years ago

8.1.0

2 years ago

5.4.2

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

7.0.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

8.0.0

2 years ago

8.2.1

2 years ago

8.2.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.3.3

3 years ago

6.0.0-alpha.1

3 years ago

5.3.2

3 years ago

5.3.1

3 years ago

5.3.0

3 years ago

5.2.1

3 years ago

5.2.0

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.1.1

3 years ago

4.0.7

3 years ago

4.1.0

3 years ago

4.0.6

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.2

3 years ago

3.4.0

3 years ago

3.3.1

3 years ago

3.3.2

3 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.1.24

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19-beta.1

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.16

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.9

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago