Handle preview api

This commit is contained in:
Sven Schmidt 2020-07-09 12:10:22 +02:00
parent fa0e8c84f5
commit 1a785581ff
3 changed files with 41 additions and 4 deletions

View file

@ -9,13 +9,22 @@ export class ContentfulDeliveryApi implements ICredentialType {
name: 'space_id', name: 'space_id',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true,
description: 'The id for the Cotentful space.' description: 'The id for the Cotentful space.'
}, },
{ {
displayName: 'Access Token', displayName: 'Content Delivery API - access token',
name: 'access_token', name: 'access_token',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true,
description: 'Access token that has access to the space'
},
{
displayName: 'Content Preview API - access token',
name: 'access_token_preview',
type: 'string' as NodePropertyTypes,
default: '',
description: 'Access token that has access to the space' description: 'Access token that has access to the space'
} }
]; ];

View file

@ -18,11 +18,22 @@ export const contentfulApiRequest = async (
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
let url = `https://cdn.contentful.com/spaces/${credentials.space_id}`; const source = that.getNodeParameter('source', 0) as string;
const isPreview = source === 'preview_api';
let accessToken = credentials.access_token as string;
if (isPreview) {
accessToken = credentials.access_token_preview as string;
console.log('accessToken', accessToken);
if (!accessToken) {
throw new Error('No access token for preview API set in credentials!');
}
}
let url = `https://${isPreview ? 'preview' : 'cdn'}.contentful.com/spaces/${credentials.space_id}`;
if (environmentId) url = `${url}/environments/${environmentId}`; if (environmentId) url = `${url}/environments/${environmentId}`;
if (endpoint) url = `${url}${endpoint}`; if (endpoint) url = `${url}${endpoint}`;
qs = qs || {}; qs = qs || {};
qs.access_token = credentials.access_token as string; qs.access_token = accessToken;
const res = await that.helpers.request!({ const res = await that.helpers.request!({
url, url,

View file

@ -33,11 +33,28 @@ export class Contentful implements INodeType {
], ],
properties: [ properties: [
// Common fields: // Common fields:
{
displayName: 'Source',
name: 'source',
type: 'options' as NodePropertyTypes,
default: 'Delivery API',
description: 'Pick where your data comes from, delivery or preview API',
options: [
{
name: 'Delivery API',
value: 'delivery_api'
},
{
name: 'Preview API',
value: 'preview_api'
}
]
},
{ {
displayName: 'Environment Id', displayName: 'Environment Id',
name: 'environment_id', name: 'environment_id',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: 'master', default: '',
description: description:
'The id for the Contentful environment (e.g. master, staging, etc.). Depending on your plan, you might not have environments. In that case use "master".' 'The id for the Contentful environment (e.g. master, staging, etc.). Depending on your plan, you might not have environments. In that case use "master".'
}, },