From 38447cbf7076ff030299385c768997685f0fa772 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Fri, 10 Feb 2023 13:24:27 -0500 Subject: [PATCH 1/6] Adds types to FetchService --- web/app/components/inputs/people-select.ts | 8 ++++---- web/app/routes/authenticated/drafts.ts | 5 ++++- web/app/routes/authenticated/settings.ts | 5 ++++- web/app/services/authenticated-user.ts | 2 ++ web/app/services/fetch.ts | 16 +++++++++++----- web/tsconfig.json | 3 ++- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/web/app/components/inputs/people-select.ts b/web/app/components/inputs/people-select.ts index 7c79962ce..f1da4e010 100644 --- a/web/app/components/inputs/people-select.ts +++ b/web/app/components/inputs/people-select.ts @@ -3,6 +3,8 @@ import { tracked } from "@glimmer/tracking"; import { inject as service } from "@ember/service"; import { task } from "ember-concurrency"; import { action } from "@ember/object"; +import FetchService from "hermes/services/fetch"; +import { assert } from "@ember/debug"; export interface Person { emailAddresses: { value: string }[]; @@ -18,9 +20,7 @@ interface PeopleSelectComponentSignature { } export default class PeopleSelectComponent extends Component { - // @ts-ignore - // FetchService not yet in the registry - @service("fetch") declare fetchSvc: any; + @service("fetch") declare fetchSvc: FetchService; /** * The list of people to display in the dropdown. @@ -66,7 +66,7 @@ export default class PeopleSelectComponent extends Component { diff --git a/web/app/routes/authenticated/drafts.ts b/web/app/routes/authenticated/drafts.ts index 8cd987511..02e0f6a03 100644 --- a/web/app/routes/authenticated/drafts.ts +++ b/web/app/routes/authenticated/drafts.ts @@ -92,7 +92,10 @@ export default class DraftsRoute extends Route { "/api/v1/drafts?" + this.createDraftURLSearchParams(params, ownerFacetOnly) ) - .then((response) => response.json()); + .then((response) => { + assert("response must be defined", response); + return response.json(); + }); return response; } catch (e: unknown) { console.error(e); diff --git a/web/app/routes/authenticated/settings.ts b/web/app/routes/authenticated/settings.ts index f75e111e1..f7b273f8a 100644 --- a/web/app/routes/authenticated/settings.ts +++ b/web/app/routes/authenticated/settings.ts @@ -4,6 +4,7 @@ import ConfigService from "hermes/services/config"; import AlgoliaService from "hermes/services/algolia"; import FetchService from "hermes/services/fetch"; import AuthenticatedUserService from "hermes/services/authenticated-user"; +import { assert } from "@ember/debug"; export default class SettingsRoute extends Route { @service("config") declare configSvc: ConfigService; @@ -15,7 +16,9 @@ export default class SettingsRoute extends Route { const allProducts = await this.fetchSvc .fetch("/api/v1/products") .then((resp) => { - return resp.json()}) + assert("response must be defined", resp); + return resp.json(); + }) .catch((err) => { console.log(`Error requesting products: ${err}`); }); diff --git a/web/app/services/authenticated-user.ts b/web/app/services/authenticated-user.ts index 149082012..81a8c5265 100644 --- a/web/app/services/authenticated-user.ts +++ b/web/app/services/authenticated-user.ts @@ -83,6 +83,8 @@ export default class AuthenticatedUserService extends Service { this.session.data.authenticated.access_token, }, }); + + assert('response must be defined', response) let subscriptions: string[] = await response.json(); let newSubscriptions: Subscription[] = []; diff --git a/web/app/services/fetch.ts b/web/app/services/fetch.ts index b413c501b..39172d1eb 100644 --- a/web/app/services/fetch.ts +++ b/web/app/services/fetch.ts @@ -1,13 +1,20 @@ -// @ts-nocheck -// TODO: Type this file. import Service from "@ember/service"; import fetch from "fetch"; import { inject as service } from "@ember/service"; +import SessionService from "./session"; + +interface FetchOptions { + method?: string; + headers?: { + [key: string]: string; + }; + body?: string; +} export default class FetchService extends Service { - @service session; + @service declare session: SessionService; - async fetch(url, options = {}) { + async fetch(url: string, options: FetchOptions = {}) { // Add the Google access token in a header (for auth) if the URL starts with // a frontslash, which will only target the application backend. if (Array.from(url)[0] == "/") { @@ -18,7 +25,6 @@ export default class FetchService extends Service { }; } try { - const resp = await fetch(url, options); if (!resp.ok) { diff --git a/web/tsconfig.json b/web/tsconfig.json index 469783720..f6efa5067 100644 --- a/web/tsconfig.json +++ b/web/tsconfig.json @@ -12,7 +12,8 @@ "hermes/tests/*": ["tests/*"], "hermes/*": ["app/*"], "hermes/mirage/*": ["mirage/*"], - "*": ["types/*"] + "*": ["types/*"], + "fetch": ["node_modules/ember-fetch"] } }, "include": ["app/**/*", "tests/**/*", "types/**/*"] From 5b7b07eb952b96d0d9acdd7a1a2045075cc9e739 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Mon, 6 Mar 2023 21:08:02 -0500 Subject: [PATCH 2/6] Update maybeUndefined --- web/app/components/inputs/people-select.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web/app/components/inputs/people-select.ts b/web/app/components/inputs/people-select.ts index f1da4e010..4a91e3f96 100644 --- a/web/app/components/inputs/people-select.ts +++ b/web/app/components/inputs/people-select.ts @@ -66,7 +66,9 @@ export default class PeopleSelectComponent extends Component { From 887b717150f9417d3521bc100272c34196193726 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Tue, 7 Mar 2023 20:34:36 -0500 Subject: [PATCH 3/6] Add fetch to tsconfig --- web/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/web/tsconfig.json b/web/tsconfig.json index 6e5ecf42e..7a28cb44e 100644 --- a/web/tsconfig.json +++ b/web/tsconfig.json @@ -13,6 +13,7 @@ "hermes/*": ["app/*"], "hermes/mirage/*": ["mirage/*"], "*": ["types/*"], + "fetch": ["node_modules/ember-fetch"], "ember-cli-flash/*": ["node_modules/ember-cli-flash"] } }, From 5712506a41213cb11241375c173e53e643334594 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Tue, 7 Mar 2023 20:41:16 -0500 Subject: [PATCH 4/6] Fix type errors --- web/app/components/new/doc-form.ts | 2 +- web/app/routes/authenticated/drafts.ts | 2 +- web/app/routes/authenticated/settings.ts | 3 ++- web/app/services/authenticated-user.ts | 18 +++++++++--------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/web/app/components/new/doc-form.ts b/web/app/components/new/doc-form.ts index 46d757cc3..66418818a 100644 --- a/web/app/components/new/doc-form.ts +++ b/web/app/components/new/doc-form.ts @@ -214,7 +214,7 @@ export default class NewDocFormComponent extends Component response.json()); + .then((response) => response?.json()); // Wait for document to be available. await timeout(AWAIT_DOC_DELAY); diff --git a/web/app/routes/authenticated/drafts.ts b/web/app/routes/authenticated/drafts.ts index c11819ed4..47d6a916d 100644 --- a/web/app/routes/authenticated/drafts.ts +++ b/web/app/routes/authenticated/drafts.ts @@ -93,7 +93,7 @@ export default class DraftsRoute extends Route { "/api/v1/drafts?" + this.createDraftURLSearchParams(params, ownerFacetOnly) ) - .then((response) => response.json()); + .then((response) => response?.json()); return response; } catch (e: unknown) { console.error(e); diff --git a/web/app/routes/authenticated/settings.ts b/web/app/routes/authenticated/settings.ts index f75e111e1..2db115999 100644 --- a/web/app/routes/authenticated/settings.ts +++ b/web/app/routes/authenticated/settings.ts @@ -15,7 +15,8 @@ export default class SettingsRoute extends Route { const allProducts = await this.fetchSvc .fetch("/api/v1/products") .then((resp) => { - return resp.json()}) + return resp?.json(); + }) .catch((err) => { console.log(`Error requesting products: ${err}`); }); diff --git a/web/app/services/authenticated-user.ts b/web/app/services/authenticated-user.ts index 69239e018..afb1f72b1 100644 --- a/web/app/services/authenticated-user.ts +++ b/web/app/services/authenticated-user.ts @@ -77,19 +77,19 @@ export default class AuthenticatedUserService extends Service { */ fetchSubscriptions = task(async () => { try { - let response = await this.fetchSvc.fetch("/api/v1/me/subscriptions", { - method: "GET", - headers: { - "Hermes-Google-Access-Token": - this.session.data.authenticated.access_token, - }, - }); - let subscriptions: string[] = await response.json(); + let subscriptions = await this.fetchSvc + .fetch("/api/v1/me/subscriptions", { + method: "GET", + headers: { + "Hermes-Google-Access-Token": + this.session.data.authenticated.access_token, + }, + }) + .then((response) => response?.json()); let newSubscriptions: Subscription[] = []; if (subscriptions) { - // map newSubscriptions = subscriptions.map((subscription: string) => { return { productArea: subscription, From b0e88ce3d4c2dc6f1d8a7da73a20821431cc4863 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Tue, 7 Mar 2023 20:48:25 -0500 Subject: [PATCH 5/6] Update people-select.ts --- web/app/components/inputs/people-select.ts | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/web/app/components/inputs/people-select.ts b/web/app/components/inputs/people-select.ts index ce808a9c2..cccfcb33c 100644 --- a/web/app/components/inputs/people-select.ts +++ b/web/app/components/inputs/people-select.ts @@ -58,20 +58,18 @@ export default class PeopleSelectComponent extends Component { try { - const res = await this.fetchSvc.fetch("/api/v1/people", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - query: query, - }), - }); + const people = await this.fetchSvc + .fetch("/api/v1/people", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: query, + }), + }) + .then((response) => response?.json()); - assert('response expected', res); - - const peopleJson = await res.json(); - - if (peopleJson) { - this.people = peopleJson.map((p: GoogleUser) => { + if (people) { + this.people = people.map((p: GoogleUser) => { return { email: p.emailAddresses[0]?.value, imgURL: p.photos?.[0]?.url, From 618b12110137aa0d99838d0379aefcb86719a1e8 Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Mon, 13 Mar 2023 14:17:45 -0400 Subject: [PATCH 6/6] Remove unnecessary import --- web/app/routes/authenticated/settings.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/web/app/routes/authenticated/settings.ts b/web/app/routes/authenticated/settings.ts index ed7a82c52..2db115999 100644 --- a/web/app/routes/authenticated/settings.ts +++ b/web/app/routes/authenticated/settings.ts @@ -4,7 +4,6 @@ import ConfigService from "hermes/services/config"; import AlgoliaService from "hermes/services/algolia"; import FetchService from "hermes/services/fetch"; import AuthenticatedUserService from "hermes/services/authenticated-user"; -import { assert } from "@ember/debug"; export default class SettingsRoute extends Route { @service("config") declare configSvc: ConfigService;