@ovok/core 0.4.49-dev.525 → 0.4.49-dev.527

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ exports.OVOK_CORE_REQUIREMENTS = {
26
26
  description: "The FHIR surface a server must provide for the @ovok/core client library's own methods to function.",
27
27
  software: {
28
28
  name: "@ovok/core",
29
- version: "0.4.49-dev.525+7eb412c",
29
+ version: "0.4.49-dev.527+15b44e2",
30
30
  },
31
31
  fhirVersion: "4.0.1",
32
32
  format: ["json"],
@@ -15,6 +15,7 @@ export type { SessionChangeListener } from "./auth/user-session";
15
15
  export type { LoginCredentials, LoginResult, MfaChallenge, ProfileType } from "./resources/auth-resource";
16
16
  export type { CreateOptions, PatchOperation } from "./resources/fhir-resource";
17
17
  export type { HealthCheck } from "./resources/health-resource";
18
+ export type { AdoptedSession } from "./resources/auth/adopted-session";
18
19
  export type { PatientRegistration, PractitionerRegistration, RegisterResult } from "./resources/auth/registration";
19
20
  export type { SocialCredentials, SocialProvider } from "./resources/auth/social-login";
20
21
  export type { LastObservationsQuery, LastnQuery, LatestObservationsParams, MeasurementCodeTypes, SaveObservationsOptions, SaveObservationsResult, } from "./resources/measurements/measurements-resource";
@@ -0,0 +1,30 @@
1
+ import type { LoginTokens } from "../../auth/session";
2
+ /**
3
+ * A sign-in made elsewhere, such as the session an earlier client stored, for `ovok.auth.adoptSession` to continue.
4
+ *
5
+ * @category auth
6
+ */
7
+ export type AdoptedSession = {
8
+ /** Bearer token for API requests. */
9
+ accessToken: string;
10
+ /** Token that renews the session; null or omitted when there is none. */
11
+ refreshToken?: string | null;
12
+ /** Seconds until `accessToken` expires. Omit it when unknown: the first request then refreshes the session. */
13
+ expiresIn?: number;
14
+ /** The signed-in profile. */
15
+ profile: {
16
+ /** FHIR reference, such as `Patient/123`. */
17
+ reference: string;
18
+ /** Display name, if known. */
19
+ display?: string | null;
20
+ };
21
+ /** The project the session belongs to. Omit it when unknown. */
22
+ project?: {
23
+ /** FHIR reference, such as `Project/123`. */
24
+ reference: string;
25
+ /** Display name, if known. */
26
+ display?: string | null;
27
+ };
28
+ };
29
+ /** Checks the input at the trust boundary: it is often parsed from storage, so its types are not given. */
30
+ export declare function loginTokensFromAdopted(adopted: AdoptedSession): LoginTokens;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loginTokensFromAdopted = loginTokensFromAdopted;
4
+ const ovok_errors_1 = require("../../errors/ovok-errors");
5
+ const FHIR_REFERENCE = /^[A-Za-z]+\/[A-Za-z0-9\-.]{1,64}$/;
6
+ const DUE_AT_ONCE_SECONDS = 0;
7
+ const NO_PROJECT = { reference: "" };
8
+ /** Checks the input at the trust boundary: it is often parsed from storage, so its types are not given. */
9
+ function loginTokensFromAdopted(adopted) {
10
+ const { accessToken, refreshToken, expiresIn, profile, project } = (adopted !== null && adopted !== void 0 ? adopted : {});
11
+ return {
12
+ accessToken: requiredText("accessToken", accessToken),
13
+ refreshToken: optionalText("refreshToken", refreshToken),
14
+ expiresIn: expiresInSeconds(expiresIn),
15
+ profile: checkedReference("profile", profile),
16
+ project: project === undefined ? NO_PROJECT : checkedReference("project", project),
17
+ };
18
+ }
19
+ function reject(problem) {
20
+ throw new ovok_errors_1.OvokValidationError((0, ovok_errors_1.detailsFor)(`Cannot adopt the session: ${problem}.`));
21
+ }
22
+ const isText = (value) => typeof value === "string" && value !== "";
23
+ function requiredText(field, value) {
24
+ return isText(value) ? value : reject(`${field} must be a non-empty string`);
25
+ }
26
+ function optionalText(field, value) {
27
+ return value === undefined || value === null ? null : requiredText(field, value);
28
+ }
29
+ function expiresInSeconds(value) {
30
+ if (value === undefined) {
31
+ return DUE_AT_ONCE_SECONDS;
32
+ }
33
+ return typeof value === "number" && Number.isFinite(value) && value >= 0
34
+ ? value
35
+ : reject("expiresIn must be a number of seconds, zero or more");
36
+ }
37
+ function checkedReference(field, value) {
38
+ const { reference, display = null } = (value !== null && value !== void 0 ? value : {});
39
+ if (typeof reference !== "string" || !FHIR_REFERENCE.test(reference)) {
40
+ reject(`${field}.reference must be a FHIR reference like ResourceType/id`);
41
+ }
42
+ if (display !== null && typeof display !== "string") {
43
+ reject(`${field}.display must be a string or null`);
44
+ }
45
+ return { reference, display };
46
+ }
@@ -10,6 +10,7 @@ import { Session } from "../auth/session";
10
10
  import { UserSession } from "../auth/user-session";
11
11
  import { HttpClient } from "../http/http-client";
12
12
  import { RequestOptions } from "../http/transmit";
13
+ import { AdoptedSession } from "./auth/adopted-session";
13
14
  import { AuthSessionsResource } from "./auth/auth-sessions-resource";
14
15
  import { PatientRegistration, PractitionerRegistration, RegisterResult } from "./auth/registration";
15
16
  import { SocialCredentials, SocialProvider } from "./auth/social-login";
@@ -96,6 +97,21 @@ export declare class AuthResource {
96
97
  registerPractitioner(details: PractitionerRegistration, options?: RequestOptions): Promise<Session>;
97
98
  /** Signs in with an Apple or Google token. Calls `POST auth/external/{provider}`. */
98
99
  socialLogin<P extends SocialProvider>(provider: P, credential: SocialCredentials[P], options?: RequestOptions): Promise<Session>;
100
+ /**
101
+ * Continues a sign-in made elsewhere, such as the session an earlier client stored, as if the user had just signed in.
102
+ * It replaces any current session, is saved and announced like a login, and refreshes like one.
103
+ *
104
+ * @example
105
+ * const session = await ovok.auth.adoptSession({
106
+ * accessToken,
107
+ * refreshToken,
108
+ * profile: { reference: "Patient/123" },
109
+ * });
110
+ *
111
+ * @throws OvokValidationError when the access token or profile reference is missing or malformed, or another field is invalid.
112
+ * @throws OvokConfigError when the client was created with `clientSecret`.
113
+ */
114
+ adoptSession(adopted: AdoptedSession): Promise<Session>;
99
115
  /** Returns the current session, or null when signed out. */
100
116
  current(): Promise<Session | null>;
101
117
  /** Fetches the signed-in user's profile. Calls `GET auth/me`. */
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuthResource = exports.ME_PATH = void 0;
4
4
  const generate_code_challenge_and_verifier_1 = require("../../client/auth/generate-code-challenge-and-verifier");
5
5
  const ovok_errors_1 = require("../errors/ovok-errors");
6
+ const adopted_session_1 = require("./auth/adopted-session");
6
7
  const auth_sessions_resource_1 = require("./auth/auth-sessions-resource");
7
8
  const registration_1 = require("./auth/registration");
8
9
  const social_login_1 = require("./auth/social-login");
@@ -39,7 +40,7 @@ class AuthResource {
39
40
  const { profileType, email, password } = credentials;
40
41
  const step = await this.http
41
42
  .anonymous()
42
- .post(loginPath(profileType, "start"), { email, password, codeChallenge }, options);
43
+ .post(loginPath(profileType, "start"), { email, password, codeChallenge, tenantCode: this.tenantCode }, options);
43
44
  if (step.nextStep === "mfa") {
44
45
  return { status: "mfa-required", challenge: { profileType, loginId: step.loginId, codeVerifier } };
45
46
  }
@@ -51,7 +52,7 @@ class AuthResource {
51
52
  const { profileType, loginId, codeVerifier } = challenge;
52
53
  const step = await this.http
53
54
  .anonymous()
54
- .post(loginPath(profileType, "mfa"), { loginId, mfaToken: code }, options);
55
+ .post(loginPath(profileType, "mfa"), { loginId, mfaToken: code, tenantCode: this.tenantCode }, options);
55
56
  return this.exchange(session, { profileType, sessionCode: step.sessionCode, codeVerifier }, options);
56
57
  }
57
58
  /** A project that routes the email to a clinician invitation signs no one in. */
@@ -81,6 +82,24 @@ class AuthResource {
81
82
  const path = (0, social_login_1.socialLoginPath)(provider, this.socialLoginClientId(), internalId);
82
83
  return session.begin(await this.http.anonymous().post(path, providerResult, options));
83
84
  }
85
+ /**
86
+ * Continues a sign-in made elsewhere, such as the session an earlier client stored, as if the user had just signed in.
87
+ * It replaces any current session, is saved and announced like a login, and refreshes like one.
88
+ *
89
+ * @example
90
+ * const session = await ovok.auth.adoptSession({
91
+ * accessToken,
92
+ * refreshToken,
93
+ * profile: { reference: "Patient/123" },
94
+ * });
95
+ *
96
+ * @throws OvokValidationError when the access token or profile reference is missing or malformed, or another field is invalid.
97
+ * @throws OvokConfigError when the client was created with `clientSecret`.
98
+ */
99
+ async adoptSession(adopted) {
100
+ const session = this.userSession();
101
+ return session.begin((0, adopted_session_1.loginTokensFromAdopted)(adopted));
102
+ }
84
103
  /** Returns the current session, or null when signed out. */
85
104
  async current() {
86
105
  return this.session === null ? null : this.session.current();
@@ -4,6 +4,8 @@ import { RequestOptions } from "../../http/transmit";
4
4
  import { CmsReadParams } from "./cms-delivery";
5
5
  import { CmsDocumentsResource } from "./cms-documents-resource";
6
6
  import { ReleaseNotesResource } from "./release-notes-resource";
7
+ /** 25,000 documents: far beyond any real collection, so reaching it means the CMS never ends its pages. */
8
+ export declare const MAX_TRANSLATION_PAGES = 500;
7
9
  /**
8
10
  * Published CMS content: documents, legal pages, release notes and translations. Calls routes under `v1/public/cms`.
9
11
  *
@@ -18,7 +20,12 @@ export declare class CmsResource {
18
20
  constructor(http: HttpClient, tenantCode: string);
19
21
  /** "terms-and-conditions", "data-privacy", …: readable before sign-in. */
20
22
  legalPage(slug: string, params?: CmsReadParams, options?: RequestOptions): Promise<OvokCmsDocument>;
21
- /** The `translations` collection as one tree, readable before sign-in; nothing is kept on the device. */
23
+ /**
24
+ * The `translations` collection as one tree, readable before sign-in; nothing is kept on the device.
25
+ * Reads every page of the collection.
26
+ *
27
+ * @throws OvokError with code `cms_translations_page_limit` when the CMS is still offering pages after 500, rather than return a partial tree.
28
+ */
22
29
  translations(locale?: string, options?: RequestOptions): Promise<OvokTranslations>;
23
30
  private translationDocuments;
24
31
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CmsResource = void 0;
3
+ exports.CmsResource = exports.MAX_TRANSLATION_PAGES = void 0;
4
+ const ovok_errors_1 = require("../../errors/ovok-errors");
4
5
  const cms_delivery_1 = require("./cms-delivery");
5
6
  const cms_documents_resource_1 = require("./cms-documents-resource");
6
7
  const release_notes_resource_1 = require("./release-notes-resource");
@@ -9,7 +10,9 @@ const LEGAL_PAGES = "legal-pages";
9
10
  const TRANSLATIONS = "translations";
10
11
  const FIRST_PAGE = 1;
11
12
  const LARGEST_PAGE_SIZE = 50;
12
- const MAX_TRANSLATION_PAGES = 20;
13
+ /** 25,000 documents: far beyond any real collection, so reaching it means the CMS never ends its pages. */
14
+ exports.MAX_TRANSLATION_PAGES = 500;
15
+ const TRANSLATION_PAGE_LIMIT_CODE = "cms_translations_page_limit";
13
16
  /**
14
17
  * Published CMS content: documents, legal pages, release notes and translations. Calls routes under `v1/public/cms`.
15
18
  *
@@ -25,19 +28,29 @@ class CmsResource {
25
28
  legalPage(slug, params = {}, options) {
26
29
  return this.documents.retrieve(LEGAL_PAGES, slug, params, options);
27
30
  }
28
- /** The `translations` collection as one tree, readable before sign-in; nothing is kept on the device. */
31
+ /**
32
+ * The `translations` collection as one tree, readable before sign-in; nothing is kept on the device.
33
+ * Reads every page of the collection.
34
+ *
35
+ * @throws OvokError with code `cms_translations_page_limit` when the CMS is still offering pages after 500, rather than return a partial tree.
36
+ */
29
37
  async translations(locale, options) {
30
38
  return (0, translation_tree_1.translationTree)(await this.translationDocuments(locale, options));
31
39
  }
32
40
  async translationDocuments(locale, options) {
33
41
  const documents = [];
34
- for (let page = FIRST_PAGE, hasNextPage = true; hasNextPage && page <= MAX_TRANSLATION_PAGES; page++) {
42
+ for (let page = FIRST_PAGE; page <= exports.MAX_TRANSLATION_PAGES; page++) {
35
43
  const params = { locale, page, limit: LARGEST_PAGE_SIZE };
36
44
  const result = await this.delivery.listPage(TRANSLATIONS, params, options);
37
45
  documents.push(...result.docs);
38
- hasNextPage = result.hasNextPage;
46
+ if (!result.hasNextPage) {
47
+ return documents;
48
+ }
39
49
  }
40
- return documents;
50
+ throw new ovok_errors_1.OvokError({
51
+ ...(0, ovok_errors_1.detailsFor)(`The CMS still had translation pages after ${exports.MAX_TRANSLATION_PAGES}; none were returned.`),
52
+ code: TRANSLATION_PAGE_LIMIT_CODE,
53
+ });
41
54
  }
42
55
  }
43
56
  exports.CmsResource = CmsResource;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ovok/core",
3
- "version": "0.4.49-dev.525+7eb412c",
3
+ "version": "0.4.49-dev.527+15b44e2",
4
4
  "private": false,
5
5
  "main": "dist/next/index.js",
6
6
  "types": "dist/next/index.d.ts",