-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from cosmicjs/staging
v1.1.0: Props graph syntax, media data
- Loading branch information
Showing
12 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@cosmicjs/sdk': minor | ||
--- | ||
|
||
Adds: props graph syntax to Objects fetching, media data fetching option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
node_modules | ||
dist | ||
|
||
test.* | ||
test.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ src | |
.prettierrc.js | ||
.nvmrc | ||
tsconfig.json | ||
|
||
test.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
import { PromiseFnType } from '../../../../types/promise.types'; | ||
import { promiserTryCatchWrapper } from '../../../../utils/request.promiser'; | ||
import Chaining from './chaining'; | ||
import { addFullMediaData } from '../../../../utils/addFullMedia'; | ||
import { BucketConfig } from '../../../../types/config.types'; | ||
import { createBucketClient } from '../..'; | ||
|
||
export default class FindOneChaining extends Chaining { | ||
private bucketConfig: BucketConfig; | ||
|
||
constructor(endpoint: string, bucketConfig: BucketConfig) { | ||
super(endpoint); | ||
this.bucketConfig = bucketConfig; | ||
} | ||
|
||
async then<FulfilledResult = any, RejectedResult = never>( | ||
onFulfilled?: PromiseFnType<FulfilledResult>, | ||
onRejected?: PromiseFnType<RejectedResult> | ||
) { | ||
await promiserTryCatchWrapper(this.endpoint, onRejected, (res) => { | ||
onFulfilled?.({ | ||
object: res.objects && res.objects.length ? res.objects[0] : null, | ||
}); | ||
await promiserTryCatchWrapper(this.endpoint, onRejected, async (res) => { | ||
let object = res.objects && res.objects.length ? res.objects[0] : null; | ||
if (this.opts && this.opts.media && object) { | ||
object = await addFullMediaData( | ||
object, | ||
createBucketClient(this.bucketConfig), | ||
this.opts.media.props | ||
); | ||
} | ||
|
||
onFulfilled?.({ object }); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const fetchMediaData = async ( | ||
cosmic: any, | ||
filenames: string[], | ||
props: string | ||
) => { | ||
const query = { | ||
name: { $in: filenames }, | ||
}; | ||
const { media } = await cosmic.media | ||
.find(query) | ||
.props(!props || props === 'all' ? '' : `name,url,imgix_url,${props}`); | ||
return media; | ||
}; | ||
|
||
const extractMediaFiles = (obj: any): string[] => { | ||
const mediaFiles: string[] = []; | ||
JSON.stringify(obj, (_, value) => { | ||
if (value && typeof value === 'object') { | ||
const url = value.imgix_url || value.url; | ||
if (url) { | ||
mediaFiles.push(url.split('/').pop().split('?')[0]); | ||
} | ||
} | ||
return value; | ||
}); | ||
return [...new Set(mediaFiles)]; | ||
}; | ||
|
||
const mapMediaDataToResponse = ( | ||
response: any, | ||
mediaData: any[], | ||
props: string | ||
) => { | ||
const mediaMap = new Map(mediaData.map((item) => [item.name, item])); | ||
|
||
const addFullMedia = (obj: any) => { | ||
if (obj && typeof obj === 'object') { | ||
Object.keys(obj).forEach((key) => { | ||
if (obj[key] && typeof obj[key] === 'object') { | ||
const url = obj[key].imgix_url || obj[key].url; | ||
if (url) { | ||
const filename = url.split('/').pop().split('?')[0]; | ||
if (mediaMap.has(filename)) { | ||
// eslint-disable-next-line no-param-reassign | ||
if (!props.includes('name')) { | ||
delete mediaMap.get(filename).name; | ||
} | ||
const newObj = { ...mediaMap.get(filename) }; | ||
Object.assign(obj[key], newObj); | ||
} | ||
} | ||
addFullMedia(obj[key]); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
addFullMedia(response); | ||
}; | ||
|
||
const addFullMediaData = async (response: any, cosmic: any, props: string) => { | ||
const processItem = async (item: any) => { | ||
const mediaFiles = extractMediaFiles(item); | ||
if (mediaFiles.length > 0) { | ||
const mediaData = await fetchMediaData(cosmic, mediaFiles, props); | ||
mapMediaDataToResponse(item, mediaData, props); | ||
} | ||
return item; | ||
}; | ||
|
||
if (Array.isArray(response)) { | ||
return Promise.all(response.map((item) => processItem(item))); | ||
} | ||
return processItem(response); | ||
}; | ||
|
||
export { addFullMediaData }; |