Skip to content

Commit

Permalink
refactor: switch case for response code
Browse files Browse the repository at this point in the history
  • Loading branch information
emielwit committed Sep 13, 2023
1 parent 11d1c5e commit 6570cf1
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions src/functions/publishAppFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,58 @@ const uploadAppFunctions = async (
Authorization: `Bearer ${fusionAuth.jwt()}`,
},
}).then(async (res) => {
if (res.status === 401 || res.status === 403) {
await fusionAuth.ensureLogin();
return uploadAppFunctions(functionDefinitionsFile, functionsJson, config);
switch (res.status) {
case 401:
case 403: {
await fusionAuth.ensureLogin();
return uploadAppFunctions(
functionDefinitionsFile,
functionsJson,
config,
);
}
case 201: {
const { created, updated, deleted, compiled } =
(await res.json()) as PublishResponse;

created.forEach((result) => logResult(result, 'Create:'));
updated.forEach((result) => logResult(result, 'Update:'));
deleted.forEach((result) => logResult(result, 'Delete:'));

if (!config.skipCompile) {
const compiledStatus = compiled ? 'ok' : 'error';
logResult(
{ status: compiledStatus, name: 'triggered' },
'Compilation',
);
}

return {
success: true,
message: 'Your functions are published to your application.',
};
}
case 409: {
const { created, updated, deleted, message } =
(await res.json()) as PublishResponse;

created.forEach((result) => logResult(result, 'Create:'));
updated.forEach((result) => logResult(result, 'Update:'));
deleted.forEach((result) => logResult(result, 'Delete:'));

return {
success: false,
message: message || '409 Conflict',
};
}

default:
throw new Error(
`Couldn't publish functions, Error: ${
res.status
},${await res.text()}`,
);
}

if (res.status !== 201 && res.status !== 409) {
throw new Error(
`Couldn't publish functions, Error: ${res.status},${await res.text()}`,
);
}

const { created, updated, deleted, compiled, message } =
(await res.json()) as PublishResponse;

created.forEach((result) => logResult(result, 'Create:'));
updated.forEach((result) => logResult(result, 'Update:'));
deleted.forEach((result) => logResult(result, 'Delete:'));

if (!config.skipCompile) {
const compiledStatus = compiled ? 'ok' : 'error';
logResult({ status: compiledStatus, name: 'triggered' }, 'Compilation');
}

return {
success: res.status === 201,
message: message || 'Your functions are published to your application.',
};
});
};

Expand Down

0 comments on commit 6570cf1

Please sign in to comment.