Skip to content

Commit

Permalink
poc validated
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart committed Jan 17, 2024
1 parent dbb43e8 commit 01e73ce
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 14 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/ui/atomic/create-atomic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typescript": "4.9.5"
},
"devDependencies": {
"fs-extra": "11.1.1"
"fs-extra": "11.1.1",
"@types/node": "20.11.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import defaultSearchInterface from './default-search-interface.js';

export const defaultPageManifest: IManifest = {
config: {
title: 'Atomic Stencil Project',
name: 'Atomic Stencil Project',
},
markup: defaultSearchInterface,
results: {
Expand Down
74 changes: 66 additions & 8 deletions packages/ui/atomic/create-atomic/src/fetch-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ import {
* @coveo/platform-client's IManifestResponse with simplified configuration
*/
export interface IManifest extends Omit<IManifestResponse, 'config'> {
config: Pick<ISearchInterfaceConfigurationResponse, 'title'>;
config: Pick<ISearchInterfaceConfigurationResponse, 'name'>;
}

export async function fetchPageManifest(
client: PlatformClient,
pageId: string
pageId: string,
type: 'next-gen' | 'legacy' | 'unknown',
apiKey: string,
orgId: string,
platformUrl: string
) {
const manifest = await client.searchInterfaces.manifest(pageId, {
pagePlaceholders: {
results: '--results--',
},
});
return replaceResultsPlaceholder(manifest);
let manifestGetters = [];
// if (type !== 'next-gen') {
// manifestGetters.push(getLegacyManifest);
// }
if (type !== 'legacy') {
manifestGetters.push(getNextGenManifest);
}
for (const manifestGetter of manifestGetters) {
let manifest: IManifest;
try {
manifest = await manifestGetter(
client,
pageId,
apiKey,
orgId,
platformUrl
);
} catch (error) {
continue;
}
return replaceResultsPlaceholder(manifest);
}
throw 'PANIC';
}

function replaceResultsPlaceholder(manifestResponse: IManifest) {
Expand All @@ -34,3 +55,40 @@ function replaceResultsPlaceholder(manifestResponse: IManifest) {

return manifestResponse;
}

async function getLegacyManifest(
client: PlatformClient,
pageId: string
): Promise<IManifest> {
return await client.searchInterfaces.manifest(pageId, {
pagePlaceholders: {
results: '--results--',
},
});
}

async function getNextGenManifest(
client: PlatformClient,
pageId: string,
apiKey: string,
orgId: string,
platformUrl: string
): Promise<IManifest> {
return (
await fetch(
`http://localhost:8222/rest/organizations/${orgId}/searchpage/v1/interfaces/${pageId}/manifest`,
{
headers: {
Authorization: `Bearer ${apiKey}`,
},
body: {
//@ts-ignore shush, it works.
pagePlaceholders: {
results: '--results--',
},
},
method: 'POST',
}
)
).json() as Promise<IManifest>;
}
16 changes: 12 additions & 4 deletions packages/ui/atomic/create-atomic/src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ export default function (plop: NodePlopAPI) {
...(await listSearchPagesOptions(initPlatformClient(answers))),
];
},
validate: async (input, answers: PlopData) => {
validate: async (input: string, answers: PlopData) => {
try {
return !!(await fetchPageManifest(
initPlatformClient(answers),
input
input,
'unknown',
answers['api-key'],
answers['org-id'],
answers['platform-environment']
));
} catch (error) {
return `The search page with the id "${input}" does not exist.`;
Expand All @@ -149,9 +153,13 @@ export default function (plop: NodePlopAPI) {
if (answers['page-id']) {
answers.page = await fetchPageManifest(
initPlatformClient(answers),
answers['page-id']
answers['page-id'],
'unknown',
answers['api-key'],
answers['org-id'],
answers['platform-url']
);
return `Hosted search page named "${answers.page.config.title}" has been downloaded`;
return `Hosted search page named "${answers.page.config.name}" has been downloaded`;
}

answers.page = defaultPageManifest;
Expand Down

0 comments on commit 01e73ce

Please sign in to comment.