npm.io
1.20.0 • Published 3 months ago

@newskit-render/api

Licence
UNLICENSED
Version
1.20.0
Deps
4
Size
95 kB
Vulns
2
Weekly
0

@newskit-render/api

Helper package to make setting up and using Apollo Client with CPS (formerly Main) or NewsKit API easier.

Usage

createApolloClient

import { createApolloClient, ClientTypes } from '@newskit-render/api'

// MAIN with cookies
const mainApolloClient = await createApolloClient(
  ClientTypes.main,
  context.req.headers.cookie
)

// NKAPI with cookies
const nkApiApolloClient = await createApolloClient(
  ClientTypes.nkapi,
  context.req.headers.cookie
)

// NKAPI without cookies
const apolloClient = await createApolloClient(ClientTypes.nkapi)

// pass custom set up
const apolloClient = await createApolloClient({
  initialState: {},
  uri: process.env.NEWSKIT_API_ENV_URL,
  headers: {
    'x-api-key': process.env.NEWSKIT_API_X_API_KEY,
  },
  addTypename: false,
})

getApolloClient

import { getApolloClient } from '@newskit-render/api'

const shouldUseNewskitApi = true

const apolloClient = await getApolloClient(
  shouldUseNewskitApi,
  req.headers.cookie
)

helpers

  1. getHeaders - creates header for MAIN
  2. getAccessToken - access header used by MAIN
  3. getMainSession - get MAIN session from cookie
  4. sessionParser - decodes session id

Authentication environment variables

For MAIN and NewsKit API authentication see MAIN and NewsKit API. For Step-up MFA see @newskit-render/my-account