Skip to content

Commit

Permalink
Fixed cloud integration issues (#6627)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-deshpande authored Sep 22, 2021
1 parent b0cba17 commit 47cee8c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-roses-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Keystone Cloud assets integration has been fixed
2 changes: 0 additions & 2 deletions packages/keystone/src/lib/context/createImagesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function createImagesContext(config: KeystoneConfig): ImagesContext | und
const {
apiKey = '',
imagesDomain = '',
graphqlApiEndpoint = '',
restApiEndpoint = '',
} = experimental?.keystoneCloud || {};

Expand All @@ -66,7 +65,6 @@ export function createImagesContext(config: KeystoneConfig): ImagesContext | und
return await buildKeystoneCloudImageSrc({
apiKey,
imagesDomain,
graphqlApiEndpoint,
filename,
});
}
Expand Down
48 changes: 11 additions & 37 deletions packages/keystone/src/lib/keystone-cloud/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@ import fetch from 'node-fetch';
import FormData from 'form-data';
import { ImageMetadata } from '../../types';

/**
* This function and request to the cloud API will not be necessary if we can
* encode the project's image subdomain in the apiKey.
*/
const getImagesSubdomain = async ({
apiKey,
graphqlApiEndpoint,
}: {
apiKey: string;
graphqlApiEndpoint: string;
}): Promise<string> => {
const response = await fetch(graphqlApiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query($apiKey:String) {
allImgixSources(where: { project: { apiKeys: { apiKey: $apiKey } } }) {
domain
}
}
`,
variables: {
apiKey,
},
}),
});
const json = await response.json();
const { data } = json;
const { allImgixSources } = data;
const getImagesSubdomain = ({ apiKey }: { apiKey: string }): string => {
const [, domain] = apiKey.split('__IMAGES_DOMAIN__');

return allImgixSources[0].domain;
if (!domain) {
throw new Error(
'Your API key is outdated, please regenerate it and add the new key to your Keystone config'
);
}

return domain;
};

const getS3Bucket = async ({
Expand All @@ -54,7 +30,7 @@ const getS3Bucket = async ({
body: JSON.stringify({
query: `
query($apiKey:String) {
allAmazonS3Buckets(where: { project: { apiKeys: { apiKey: $apiKey } } }) {
allAmazonS3Buckets(where: { project: { apiKey: { apiKey: $apiKey } } }) {
bucketName,
prefix,
region
Expand Down Expand Up @@ -105,16 +81,14 @@ const uploadAsset = async ({

export const buildKeystoneCloudImageSrc = async ({
apiKey,
graphqlApiEndpoint,
filename,
imagesDomain,
}: {
apiKey: string;
graphqlApiEndpoint: string;
filename: string;
imagesDomain: string;
}) => {
const imagesSubdomain = await getImagesSubdomain({ apiKey, graphqlApiEndpoint });
const imagesSubdomain = getImagesSubdomain({ apiKey });

return `http://${imagesSubdomain}.${imagesDomain}/${filename}`;
};
Expand Down

0 comments on commit 47cee8c

Please sign in to comment.