Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ScopeyNZ committed Nov 18, 2021
1 parent 657a0a5 commit 7195a98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
29 changes: 13 additions & 16 deletions dist/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,26 @@ const handler = (request, context) => __awaiter(void 0, void 0, void 0, function
}, {});
// Find or create app definitions for each org
const appDefinitionsByOrg = {};
const baseGotOptions = {
responseType: 'json',
headers: {
Authorization: `Bearer ${config.contentManagementApiKey}`,
'Content-Type': 'application/vnd.contentful.management.v1+json',
},
};
const definitionResolutionPromises = Object.keys(spacesByOrg).map((orgId) => __awaiter(void 0, void 0, void 0, function* () {
const definitionBaseUrl = `https://api.contentful.com/organizations/${orgId}/app_definitions`;
const gotOptions = {
headers: {
Authorization: `Bearer ${config.contentManagementApiKey}`,
'Content-Type': 'application/vnd.contentful.management.v1+json',
},
};
// Avoid adding a Commerce.js app if one is already installed by searching the existing definitions
const { items } = yield context.got(definitionBaseUrl, gotOptions).json();
const { items } = yield context.got(definitionBaseUrl, baseGotOptions).json();
// Look for an existing definition
const result = items.find((definition) => definition.src === appUrl);
const result = items.find((definition) => definition.src.startsWith(appUrl));
if (result) {
appDefinitionsByOrg[orgId] = result;
return;
}
// Create a definition in other cases
const options = {
method: 'PUT',
method: 'POST',
responseType: 'json',
json: {
name: 'Commerce.js App',
Expand Down Expand Up @@ -98,17 +99,13 @@ const handler = (request, context) => __awaiter(void 0, void 0, void 0, function
return acc.concat(spaceIds.map((spaceId) => __awaiter(void 0, void 0, void 0, function* () {
const appDefinition = appDefinitionsByOrg[orgId];
const url = `https://api.contentful.com/spaces/${spaceId}/environments/${config.environmentName}/app_installations/${appDefinition.sys.id}`;
const options = {
method: 'PUT',
responseType: 'json',
json: {
const options = Object.assign(Object.assign({}, baseGotOptions), { method: 'PUT', json: {
parameters: {
publicKey: context.publicKey,
},
},
};
} });
const response = yield context.got(url, options);
if (response.statusCode !== 201) {
if (response.statusCode < 200 || response.statusCode >= 300) {
// Relay the error message from Contentful
throw new Error(JSON.stringify(response.body));
}
Expand Down
24 changes: 13 additions & 11 deletions integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ const handler: IntegrationHandler = async (request, context) => {
// Find or create app definitions for each org
const appDefinitionsByOrg: { [orgId: string]: AppDefinition } = {};

const baseGotOptions: OptionsOfJSONResponseBody = {
responseType: 'json',
headers: {
Authorization: `Bearer ${config.contentManagementApiKey}`,
'Content-Type': 'application/vnd.contentful.management.v1+json',
},
};

const definitionResolutionPromises = Object.keys(spacesByOrg).map(async (orgId) => {
const definitionBaseUrl = `https://api.contentful.com/organizations/${orgId}/app_definitions`;
const gotOptions = {
headers: {
Authorization: `Bearer ${config.contentManagementApiKey}`,
'Content-Type': 'application/vnd.contentful.management.v1+json',
},
};

// Avoid adding a Commerce.js app if one is already installed by searching the existing definitions
const { items }: { items: Array<any> } = await context.got(definitionBaseUrl, gotOptions).json();
const { items }: { items: Array<any> } = await context.got(definitionBaseUrl, baseGotOptions).json();

// Look for an existing definition
const result = items.find(
(definition: AppDefinition) => definition.src === appUrl
(definition: AppDefinition) => definition.src.startsWith(appUrl)
);

if (result) {
Expand All @@ -72,7 +74,7 @@ const handler: IntegrationHandler = async (request, context) => {

// Create a definition in other cases
const options: OptionsOfJSONResponseBody = {
method: 'PUT',
method: 'POST',
responseType: 'json',
json: {
name: 'Commerce.js App',
Expand Down Expand Up @@ -116,8 +118,8 @@ const handler: IntegrationHandler = async (request, context) => {
const appDefinition = appDefinitionsByOrg[orgId];
const url = `https://api.contentful.com/spaces/${spaceId}/environments/${config.environmentName}/app_installations/${appDefinition.sys.id}`;
const options: OptionsOfJSONResponseBody = {
...baseGotOptions,
method: 'PUT',
responseType: 'json',
json: {
parameters: {
publicKey: context.publicKey,
Expand All @@ -127,7 +129,7 @@ const handler: IntegrationHandler = async (request, context) => {

const response = await context.got<AppInstallation>(url, options);

if (response.statusCode !== 201) {
if (response.statusCode < 200 || response.statusCode >= 300) {
// Relay the error message from Contentful
throw new Error(JSON.stringify(response.body));
}
Expand Down

1 comment on commit 7195a98

@vercel
Copy link

@vercel vercel bot commented on 7195a98 Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.