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

Commit

Permalink
Merge pull request #373 from superfaceai/feat/prepare-force-flag
Browse files Browse the repository at this point in the history
feat: prepare force flag
  • Loading branch information
Jakub-Vacek authored Nov 22, 2023
2 parents 713443a + 9270b19 commit 46d40ae
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/commands/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ describe('prepare CLI command', () => {
await instance.execute({
logger,
userError,
flags: { timeout: 123 },
flags: { timeout: 123, force: true },
args: { urlOrPath: url },
});

expect(prepareProviderJson).toHaveBeenCalledWith(
{
urlOrSource: url,
name: undefined,
options: { quiet: undefined, timeout: 123 },
options: { quiet: undefined, timeout: 123, force: true },
},
{ ux, userError }
);
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('prepare CLI command', () => {
{
urlOrSource: url,
name,
options: { quiet: undefined, timeout: 123 },
options: { quiet: undefined, timeout: 123, force: false },
},
{ ux, userError }
);
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('prepare CLI command', () => {
{
urlOrSource: fileContent,
name: 'file-docs',
options: { quiet: undefined, timeout: 123 },
options: { quiet: undefined, timeout: 123, force: false },
},
{ ux, userError }
);
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('prepare CLI command', () => {
{
urlOrSource: fileContent,
name,
options: { quiet: undefined, timeout: 123 },
options: { quiet: undefined, timeout: 123, force: false },
},
{ userError, ux }
);
Expand Down
17 changes: 16 additions & 1 deletion src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ This command prepares a Provider JSON metadata definition that can be used to ge
'When set to true command will print the indexed documentation overview. This is useful for debugging.',
default: false,
}),
force: oclifFlags.boolean({
required: false,
description:
'When set to `true`, the command will overwrite any existing Provider JSON metadata.',
default: false,
}),
timeout: oclifFlags.integer({
char: 't',
required: false,
Expand Down Expand Up @@ -135,6 +141,7 @@ This command prepares a Provider JSON metadata definition that can be used to ge
quiet: flags.quiet,
getDocs: flags.verbose,
timeout: flags.timeout,
force: flags.force ?? false,
},
},
{ userError, ux }
Expand All @@ -150,6 +157,9 @@ This command prepares a Provider JSON metadata definition that can be used to ge

const providerJsonPath = await writeProviderJson(
providerJsonResult.definition,
{
force: flags.force,
},
{
logger,
userError,
Expand Down Expand Up @@ -185,10 +195,15 @@ ${docs}`

export async function writeProviderJson(
providerJson: ProviderJson,
options:
| {
force?: boolean;
}
| undefined,
{ logger, userError }: { logger: ILogger; userError: UserError }
): Promise<string> {
// TODO: force flag
if (await exists(buildProviderPath(providerJson.name))) {
if ((await exists(buildProviderPath(providerJson.name))) && options?.force !== true) {
throw userError(`Provider ${providerJson.name} already exists.`, 1);
}

Expand Down
9 changes: 9 additions & 0 deletions src/common/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PollResponse =
| {
status: PollStatus.Failed;
failure_reason: string;
context?: string;
result_type: PollResultType;
}
| { status: PollStatus.Cancelled; result_type: PollResultType };
Expand Down Expand Up @@ -123,6 +124,14 @@ export async function pollUrl(

return result.result_url;
} else if (result.status === PollStatus.Failed) {
if (result.context === 'providerAlreadyExists') {
throw userError(
`Failed to ${getJobDescription(result.result_type)}: ${
result.failure_reason
}. Use --force to overwrite existing provider.`,
1
);
}
throw userError(
`Failed to ${getJobDescription(result.result_type)}: ${
result.failure_reason
Expand Down
32 changes: 16 additions & 16 deletions src/logic/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: true },
},
{ ux, userError }
)
Expand All @@ -61,7 +61,7 @@ describe('prepareProviderJson', () => {
});

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":true}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { getDocs: true, timeout: 123 },
options: { getDocs: true, timeout: 123, force: false },
},
{ ux, userError }
)
Expand All @@ -128,7 +128,7 @@ describe('prepareProviderJson', () => {
});

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand Down Expand Up @@ -157,14 +157,14 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
).rejects.toEqual(Error('Unexpected status code 400 received'));

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand All @@ -182,7 +182,7 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
Expand All @@ -194,7 +194,7 @@ describe('prepareProviderJson', () => {
);

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand All @@ -214,7 +214,7 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
Expand All @@ -223,7 +223,7 @@ describe('prepareProviderJson', () => {
);

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand All @@ -248,14 +248,14 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
).rejects.toEqual(error);

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand Down Expand Up @@ -287,14 +287,14 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
).rejects.toEqual(new Error('Unexpected status code 400 received'));

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand Down Expand Up @@ -333,14 +333,14 @@ describe('prepareProviderJson', () => {
{
urlOrSource: 'https://superface.ai/path/to/oas.json',
name: providerName,
options: { timeout: 123 },
options: { timeout: 123, force: false },
},
{ ux, userError }
)
).rejects.toEqual(new Error('Unexpected response received'));

expect(fetch).toHaveBeenNthCalledWith(1, '/authoring/providers', {
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider"}',
body: '{"source":"https://superface.ai/path/to/oas.json","name":"test-provider","force":false}',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
});
Expand Down
7 changes: 4 additions & 3 deletions src/logic/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ export async function prepareProviderJson(
quiet?: boolean;
getDocs?: boolean;
timeout: number;
force: boolean;
};
},
{ userError, ux }: { userError: UserError; ux: UX }
): Promise<{ definition: ProviderJson; docs?: string[] }> {
const client = SuperfaceClient.getClient();

const jobUrl = await startProviderPreparation(
{ source: urlOrSource, name },
{ source: urlOrSource, name, force: options.force },
{ client, userError }
);

Expand Down Expand Up @@ -133,15 +134,15 @@ export async function prepareProviderJson(
}

async function startProviderPreparation(
{ source, name }: { source: string; name?: string },
{ source, name, force }: { source: string; name?: string; force: boolean },
{ client, userError }: { client: ServiceClient; userError: UserError }
): Promise<string> {
const jobUrlResponse = await client.fetch(`/authoring/providers`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ source, name }),
body: JSON.stringify({ source, name, force }),
});

if (jobUrlResponse.status !== 202) {
Expand Down

0 comments on commit 46d40ae

Please sign in to comment.