From 04336fa244c6ad9fcf8de99068cff830c9e4789b Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Mon, 22 Apr 2024 11:08:44 -0400 Subject: [PATCH] (fix): try catch any snippet load failures (#689) --- fern/apis/fdr/generators.yml | 9 ++++ .../__test__/local/services/snippets.test.ts | 7 +++ .../snippets/getSnippetsService.ts | 44 ++++++++++--------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/fern/apis/fdr/generators.yml b/fern/apis/fdr/generators.yml index 28c3c89370..bc2d95c61a 100644 --- a/fern/apis/fdr/generators.yml +++ b/fern/apis/fdr/generators.yml @@ -60,6 +60,15 @@ groups: include_union_utils: true client: class_name: FdrClient + - name: fernapi/fern-typescript-node-sdk + version: 0.12.8-rc0 + output: + location: npm + url: npm.buildwithfern.com + package-name: "@fern-fern/snippet-template-sdk" + config: + namespaceExport: FdrSnippetTemplate + includeUtilsOnUnionMembers: true fiddle: audiences: diff --git a/servers/fdr/src/__test__/local/services/snippets.test.ts b/servers/fdr/src/__test__/local/services/snippets.test.ts index 32114fdc7d..40f3becfea 100644 --- a/servers/fdr/src/__test__/local/services/snippets.test.ts +++ b/servers/fdr/src/__test__/local/services/snippets.test.ts @@ -34,6 +34,7 @@ it("get snippets", async () => { // get snippets const snippets = getAPIResponse( await fdr.get({ + orgId: "acme", apiId: "foo", endpoint: { path: "/snippets/load", @@ -150,6 +151,7 @@ it("get Go snippets", async () => { const snippets = getAPIResponse( await fdr.get({ apiId: "echo", + orgId: "acme", endpoint: { path: "/snippets/load", method: FdrAPI.EndpointMethod.Post, @@ -260,6 +262,7 @@ it("get snippets with unregistered API", async () => { // get snippets const snippets = getAPIResponse( await fdr.get({ + orgId: "acme", apiId: "fresh", endpoint: { path: "/users/v1", @@ -313,6 +316,7 @@ it("load snippets", async () => { // load snippets (first page) const firstResponse = getAPIResponse( await fdr.load({ + orgId: "acme", apiId: "petstore", }), ); @@ -335,6 +339,7 @@ it("load snippets", async () => { // load snippets (second page) const secondResponse = getAPIResponse( await fdr.load({ + orgId: "acme", apiId: "petstore", sdks: [ { @@ -429,6 +434,7 @@ it("snippets apiId not found", async () => { // get not found apiId const response = await fdr.get({ + orgId: "acme", apiId: "dne", endpoint: { path: "/users/v1", @@ -472,6 +478,7 @@ it("get snippets (unauthenticated)", async () => { // get snippets const unauthedFdr = getClient({ authed: false, url: inject("url") }); const response = await unauthedFdr.get({ + orgId: "acme", apiId: "user", endpoint: { path: "/users/v1", diff --git a/servers/fdr/src/controllers/snippets/getSnippetsService.ts b/servers/fdr/src/controllers/snippets/getSnippetsService.ts index 4f73341bd3..ad42202114 100644 --- a/servers/fdr/src/controllers/snippets/getSnippetsService.ts +++ b/servers/fdr/src/controllers/snippets/getSnippetsService.ts @@ -40,31 +40,35 @@ export function getSnippetsService(app: FdrApplication): SnippetsService { const snippetsForEndpointMethod = snippetsForEndpointPath[req.body.endpoint.method]; return res.send(snippetsForEndpointMethod ?? []); } else { - const snippets: Snippet[] = []; + try { + const snippets: Snippet[] = []; - for (const sdk of req.body.sdks ?? []) { - const endpointSnippetTemplate: EndpointSnippetTemplate | null = await app.dao - .snippetTemplates() - .loadSnippetTemplate({ - loadSnippetTemplateRequest: { - orgId: apiInfo.orgId, - apiId: apiInfo.apiId, - endpointId: req.body.endpoint, - sdk, - }, + for (const sdk of req.body.sdks ?? []) { + const endpointSnippetTemplate: EndpointSnippetTemplate | null = await app.dao + .snippetTemplates() + .loadSnippetTemplate({ + loadSnippetTemplateRequest: { + orgId: apiInfo.orgId, + apiId: apiInfo.apiId, + endpointId: req.body.endpoint, + sdk, + }, + }); + if (endpointSnippetTemplate == null) { + throw new SnippetTemplateNotFoundError("Snippet not found"); + } + const templateResolver = new SnippetTemplateResolver({ + payload, + endpointSnippetTemplate, }); - if (endpointSnippetTemplate == null) { - throw new SnippetTemplateNotFoundError("Snippet not found"); + + snippets.push(templateResolver.resolve()); } - const templateResolver = new SnippetTemplateResolver({ - payload, - endpointSnippetTemplate, - }); - snippets.push(templateResolver.resolve()); + return res.send(snippets); + } catch (e) { + return res.send([]); } - - return res.send(snippets); } }, load: async (req, res) => {