@fabriktor/fx 0.0.30 → 0.0.31

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.
@@ -0,0 +1,50 @@
1
+ import type { InBill, OperationResult, PLBillablePeriodsFromDates, PLBillableSetItemQuantity, PLBillableVendorInvoiceFromBill, PLBillableYearSpan, RPBillablePeriodsFromDates, YearSpan } from "@fabriktor/schema";
2
+ import type { BillableOperator, OperationResultOf } from "@fabriktor/client";
3
+ export type BillableFXOptions = {
4
+ billable: BillableOperator;
5
+ };
6
+ export type GenerateFixedEstimateOptions = {
7
+ in: InBill;
8
+ };
9
+ export type AcceptFixedEstimateOptions = {
10
+ in: InBill;
11
+ };
12
+ export type GenerateDynamicEstimateOptions = {
13
+ in: InBill;
14
+ };
15
+ export type GenerateDynamicBillOptions = {
16
+ in: InBill;
17
+ exclude_billed?: boolean;
18
+ };
19
+ export type MarkLocationAchievedOptions = {
20
+ in: InBill;
21
+ };
22
+ export type YearSpanOptions = PLBillableYearSpan;
23
+ export type PeriodsFromDatesOptions = PLBillablePeriodsFromDates;
24
+ export type VendorInvoiceFromBillOptions = PLBillableVendorInvoiceFromBill;
25
+ export type SetItemQuantityOptions = PLBillableSetItemQuantity;
26
+ export interface BillableFXOperator {
27
+ generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<OperationResult>;
28
+ acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<OperationResult>;
29
+ generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<OperationResult>;
30
+ generateDynamicBill(opts: GenerateDynamicBillOptions): Promise<OperationResult>;
31
+ markLocationAchieved(opts: MarkLocationAchievedOptions): Promise<OperationResult>;
32
+ yearSpan(opts: YearSpanOptions): Promise<OperationResultOf<YearSpan>>;
33
+ periodsFromDates(opts: PeriodsFromDatesOptions): Promise<OperationResultOf<RPBillablePeriodsFromDates>>;
34
+ vendorInvoiceFromBill(opts: VendorInvoiceFromBillOptions): Promise<OperationResult>;
35
+ setItemQuantity(opts: SetItemQuantityOptions): Promise<void>;
36
+ }
37
+ export declare class BillableFX implements BillableFXOperator {
38
+ private billable;
39
+ constructor(opts: BillableFXOptions);
40
+ generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<OperationResult>;
41
+ acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<OperationResult>;
42
+ generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<OperationResult>;
43
+ generateDynamicBill(opts: GenerateDynamicBillOptions): Promise<OperationResult>;
44
+ markLocationAchieved(opts: MarkLocationAchievedOptions): Promise<OperationResult>;
45
+ yearSpan(opts: YearSpanOptions): Promise<OperationResultOf<YearSpan>>;
46
+ periodsFromDates(opts: PeriodsFromDatesOptions): Promise<OperationResultOf<RPBillablePeriodsFromDates>>;
47
+ vendorInvoiceFromBill(opts: VendorInvoiceFromBillOptions): Promise<OperationResult>;
48
+ setItemQuantity(opts: SetItemQuantityOptions): Promise<void>;
49
+ }
50
+ export declare function newBillableFX(opts: BillableFXOptions): BillableFX;
@@ -0,0 +1,74 @@
1
+ // Copyright (C) Fabriktor, Inc. 2025-present.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+ // not use this file except in compliance with the License. You may obtain
5
+ // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ const billTypeFixed = "fixed";
7
+ const billTypeDynamic = "dynamic";
8
+ export class BillableFX {
9
+ billable;
10
+ constructor(opts) {
11
+ this.billable = opts.billable;
12
+ }
13
+ async generateFixedEstimate(opts) {
14
+ return await this.billable.generateBill(billWith(opts.in, {
15
+ type: billTypeFixed,
16
+ is_estimate: true,
17
+ }));
18
+ }
19
+ async acceptFixedEstimate(opts) {
20
+ return await this.billable.generateBill(billWith(opts.in, {
21
+ type: billTypeFixed,
22
+ is_estimate: false,
23
+ }));
24
+ }
25
+ async generateDynamicEstimate(opts) {
26
+ return await this.billable.generateBill(billWith(opts.in, {
27
+ type: billTypeDynamic,
28
+ is_estimate: true,
29
+ }));
30
+ }
31
+ async generateDynamicBill(opts) {
32
+ return await this.billable.generateBill(billWith(opts.in, {
33
+ type: billTypeDynamic,
34
+ is_estimate: false,
35
+ exclude_billed: opts.exclude_billed,
36
+ }));
37
+ }
38
+ async markLocationAchieved(opts) {
39
+ return await this.billable.generateBill(billWith(opts.in, {
40
+ is_location_achieved: true,
41
+ }));
42
+ }
43
+ async yearSpan(opts) {
44
+ return await this.billable.yearSpan(opts);
45
+ }
46
+ async periodsFromDates(opts) {
47
+ return await this.billable.periodsFromDates(opts);
48
+ }
49
+ async vendorInvoiceFromBill(opts) {
50
+ return await this.billable.vendorInvoiceFromBill(opts);
51
+ }
52
+ async setItemQuantity(opts) {
53
+ await this.billable.setItemQuantity(opts);
54
+ }
55
+ }
56
+ export function newBillableFX(opts) {
57
+ return new BillableFX(opts);
58
+ }
59
+ function billWith(x, overrides) {
60
+ const out = {
61
+ ...x,
62
+ ...defined(overrides),
63
+ };
64
+ return out;
65
+ }
66
+ function defined(x) {
67
+ const out = {};
68
+ for (const [k, v] of Object.entries(x)) {
69
+ if (v !== undefined) {
70
+ out[k] = v;
71
+ }
72
+ }
73
+ return out;
74
+ }
@@ -0,0 +1,33 @@
1
+ import type { PLCallsMintVideoToken, PLNotificationsRelay } from "@fabriktor/schema";
2
+ import type { CallsOperator, NotificationsOperator } from "@fabriktor/client";
3
+ export type MintVideoTokenOptions = PLCallsMintVideoToken;
4
+ export type StartCallOptions = {
5
+ mint: MintVideoTokenOptions;
6
+ notification: PLNotificationsRelay;
7
+ };
8
+ export type AnswerCallOptions = {
9
+ mint: MintVideoTokenOptions;
10
+ self_notification: PLNotificationsRelay;
11
+ };
12
+ export type CancelCallOptions = {
13
+ notification: PLNotificationsRelay;
14
+ };
15
+ export type CallsFXOptions = {
16
+ calls: CallsOperator;
17
+ notifications: NotificationsOperator;
18
+ };
19
+ export interface CallsFXOperator {
20
+ startCall(opts: StartCallOptions): Promise<string>;
21
+ answerCall(opts: AnswerCallOptions): Promise<string>;
22
+ cancelCall(opts: CancelCallOptions): Promise<void>;
23
+ }
24
+ export declare class CallsFX implements CallsFXOperator {
25
+ private calls;
26
+ private notifications;
27
+ constructor(opts: CallsFXOptions);
28
+ startCall(opts: StartCallOptions): Promise<string>;
29
+ answerCall(opts: AnswerCallOptions): Promise<string>;
30
+ cancelCall(opts: CancelCallOptions): Promise<void>;
31
+ private requireVideoToken;
32
+ }
33
+ export declare function newCallsFX(opts: CallsFXOptions): CallsFX;
@@ -0,0 +1,37 @@
1
+ // Copyright (C) Fabriktor, Inc. 2025-present.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+ // not use this file except in compliance with the License. You may obtain
5
+ // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ import { errVideoTokenMintFailed } from "../core/err.js";
7
+ export class CallsFX {
8
+ calls;
9
+ notifications;
10
+ constructor(opts) {
11
+ this.calls = opts.calls;
12
+ this.notifications = opts.notifications;
13
+ }
14
+ async startCall(opts) {
15
+ const token = await this.requireVideoToken(opts.mint);
16
+ await this.notifications.relayNotification(opts.notification);
17
+ return token;
18
+ }
19
+ async answerCall(opts) {
20
+ const token = await this.requireVideoToken(opts.mint);
21
+ await this.notifications.relayNotification(opts.self_notification);
22
+ return token;
23
+ }
24
+ async cancelCall(opts) {
25
+ await this.notifications.relayNotification(opts.notification);
26
+ }
27
+ async requireVideoToken(opts) {
28
+ const out = await this.calls.mintVideoToken(opts);
29
+ if (out.value === undefined || out.value.trim() === "") {
30
+ throw errVideoTokenMintFailed();
31
+ }
32
+ return out.value;
33
+ }
34
+ }
35
+ export function newCallsFX(opts) {
36
+ return new CallsFX(opts);
37
+ }
@@ -1,26 +1,63 @@
1
1
  export declare const FXErrKind: {
2
- readonly UsernameResolveFailed: "username_resolve_failed";
3
- readonly UsernameInvalid: "username_invalid";
4
- readonly PhoneRequired: "phone_required";
5
- readonly PhoneCodeRequired: "phone_code_required";
2
+ readonly Required: "required";
3
+ readonly Validation: "validation";
4
+ readonly ResolveFailed: "resolve_failed";
6
5
  readonly Unknown: "unknown";
7
6
  };
8
7
  export type FXErrKind = (typeof FXErrKind)[keyof typeof FXErrKind];
9
- export type FXErrOptions = {
8
+ export type FieldOf<T extends object> = Extract<keyof T, string>;
9
+ export type FXErrOptions<T extends object = Record<string, unknown>> = {
10
10
  kind: FXErrKind;
11
11
  msg: string;
12
+ field?: FieldOf<T>;
13
+ resource?: string;
14
+ cause?: unknown;
15
+ };
16
+ export type RequiredFXErrOptions<T extends object = Record<string, unknown>> = {
17
+ field: FieldOf<T>;
18
+ msg: string;
19
+ cause?: unknown;
20
+ };
21
+ export type ValidationFXErrOptions<T extends object = Record<string, unknown>> = {
22
+ field?: FieldOf<T>;
23
+ msg: string;
24
+ cause?: unknown;
25
+ };
26
+ export type ResolveFailedFXErrOptions<T extends object = Record<string, unknown>> = {
27
+ resource: string;
28
+ msg: string;
29
+ field?: FieldOf<T>;
30
+ cause?: unknown;
31
+ };
32
+ export type UsernameInvalidFXErrOptions<T extends object = Record<string, unknown>> = {
33
+ field: FieldOf<T>;
34
+ msg: string;
35
+ cause?: unknown;
36
+ };
37
+ type FXErrRuntimeOptions = {
38
+ kind: FXErrKind;
39
+ msg: string;
40
+ field?: string;
41
+ resource?: string;
12
42
  cause?: unknown;
13
43
  };
14
44
  export declare class FXErr extends Error {
15
45
  readonly kind: FXErrKind;
46
+ readonly field?: string;
47
+ readonly resource?: string;
16
48
  readonly cause?: unknown;
17
- constructor(opts: FXErrOptions);
49
+ constructor(opts: FXErrRuntimeOptions);
18
50
  }
19
- export declare function errFX(opts: FXErrOptions): FXErr;
20
- export declare function errUsernameResolveFailed(): FXErr;
21
- export declare function errUsernameInvalid(msg: string): FXErr;
22
- export declare function errPhoneRequired(): FXErr;
23
- export declare function errPhoneCodeRequired(): FXErr;
51
+ export declare function errFX<T extends object = Record<string, unknown>>(opts: FXErrOptions<T>): FXErr;
52
+ export declare function errRequired<T extends object = Record<string, unknown>>(opts: RequiredFXErrOptions<T>): FXErr;
53
+ export declare function errValidation<T extends object = Record<string, unknown>>(opts: ValidationFXErrOptions<T>): FXErr;
54
+ export declare function errResolveFailed<T extends object = Record<string, unknown>>(opts: ResolveFailedFXErrOptions<T>): FXErr;
55
+ export declare function errUsernameResolveFailed<T extends object = Record<string, unknown>>(field?: FieldOf<T>): FXErr;
56
+ export declare function errVideoTokenMintFailed(): FXErr;
57
+ export declare function errUsernameInvalid<T extends object = Record<string, unknown>>(opts: UsernameInvalidFXErrOptions<T>): FXErr;
58
+ export declare function errPhoneRequired<T extends object = Record<string, unknown>>(field: FieldOf<T>): FXErr;
59
+ export declare function errPhoneCodeRequired<T extends object = Record<string, unknown>>(field: FieldOf<T>): FXErr;
24
60
  export declare function errUnknown(cause: unknown): FXErr;
25
61
  export declare function isFXErr(err: unknown): err is FXErr;
26
62
  export declare function fxErrMessage(err: unknown): string;
63
+ export {};
@@ -5,49 +5,94 @@
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
6
  const msgUnknownFXErr = "Unknown FX error.";
7
7
  const msgUnableToResolveUsername = "Unable to resolve username.";
8
+ const msgUnableToMintVideoToken = "Unable to mint video token.";
8
9
  const msgPhoneRequired = "Phone number is required.";
9
10
  const msgPhoneCodeRequired = "Verification code is required.";
11
+ const resourceUsername = "username";
12
+ const resourceVideoToken = "video_token";
10
13
  export const FXErrKind = {
11
- UsernameResolveFailed: "username_resolve_failed",
12
- UsernameInvalid: "username_invalid",
13
- PhoneRequired: "phone_required",
14
- PhoneCodeRequired: "phone_code_required",
14
+ Required: "required",
15
+ Validation: "validation",
16
+ ResolveFailed: "resolve_failed",
15
17
  Unknown: "unknown",
16
18
  };
17
19
  export class FXErr extends Error {
18
20
  kind;
21
+ field;
22
+ resource;
19
23
  cause;
20
24
  constructor(opts) {
21
25
  super(opts.msg);
22
26
  this.name = new.target.name;
23
27
  this.kind = opts.kind;
28
+ this.field = opts.field;
29
+ this.resource = opts.resource;
24
30
  this.cause = opts.cause;
25
31
  }
26
32
  }
27
33
  export function errFX(opts) {
28
- return new FXErr(opts);
34
+ return new FXErr({
35
+ kind: opts.kind,
36
+ msg: opts.msg,
37
+ field: opts.field,
38
+ resource: opts.resource,
39
+ cause: opts.cause,
40
+ });
29
41
  }
30
- export function errUsernameResolveFailed() {
42
+ export function errRequired(opts) {
31
43
  return errFX({
32
- kind: FXErrKind.UsernameResolveFailed,
33
- msg: msgUnableToResolveUsername,
44
+ kind: FXErrKind.Required,
45
+ msg: opts.msg,
46
+ field: opts.field,
47
+ cause: opts.cause,
34
48
  });
35
49
  }
36
- export function errUsernameInvalid(msg) {
50
+ export function errValidation(opts) {
37
51
  return errFX({
38
- kind: FXErrKind.UsernameInvalid,
39
- msg,
52
+ kind: FXErrKind.Validation,
53
+ msg: opts.msg,
54
+ field: opts.field,
55
+ cause: opts.cause,
40
56
  });
41
57
  }
42
- export function errPhoneRequired() {
58
+ export function errResolveFailed(opts) {
43
59
  return errFX({
44
- kind: FXErrKind.PhoneRequired,
60
+ kind: FXErrKind.ResolveFailed,
61
+ msg: opts.msg,
62
+ field: opts.field,
63
+ resource: opts.resource,
64
+ cause: opts.cause,
65
+ });
66
+ }
67
+ export function errUsernameResolveFailed(field) {
68
+ return errResolveFailed({
69
+ resource: resourceUsername,
70
+ field,
71
+ msg: msgUnableToResolveUsername,
72
+ });
73
+ }
74
+ export function errVideoTokenMintFailed() {
75
+ return errResolveFailed({
76
+ resource: resourceVideoToken,
77
+ msg: msgUnableToMintVideoToken,
78
+ });
79
+ }
80
+ export function errUsernameInvalid(opts) {
81
+ return errValidation({
82
+ field: opts.field,
83
+ msg: opts.msg,
84
+ cause: opts.cause,
85
+ });
86
+ }
87
+ export function errPhoneRequired(field) {
88
+ return errRequired({
89
+ field,
45
90
  msg: msgPhoneRequired,
46
91
  });
47
92
  }
48
- export function errPhoneCodeRequired() {
49
- return errFX({
50
- kind: FXErrKind.PhoneCodeRequired,
93
+ export function errPhoneCodeRequired(field) {
94
+ return errRequired({
95
+ field,
51
96
  msg: msgPhoneCodeRequired,
52
97
  });
53
98
  }
package/dist/src/fx.d.ts CHANGED
@@ -1,10 +1,15 @@
1
- import type { PreferencesOperator, TmpPhonesOperator, TmpUsernamesOperator, UsersOperator } from "@fabriktor/client";
1
+ import type { BillableOperator, CallsOperator, NotificationsOperator, PreferencesOperator, TmpPhonesOperator, TmpUsernamesOperator, UsersOperator } from "@fabriktor/client";
2
2
  import type { AuthOperator } from "./auth/auth.js";
3
+ import type { BillableFX, BillableFXOperator } from "./billable/billable.js";
4
+ import type { CallsFX, CallsFXOperator } from "./calls/calls.js";
3
5
  import type { PreferencesFX, PreferencesFXOperator } from "./preferences/preferences.js";
4
6
  import type { SessionFX, SessionFXOperator } from "./session/session.js";
5
7
  import type { UsersFX, UsersFXOperator } from "./users/users.js";
6
8
  export type FXOptions = {
7
9
  auth: AuthOperator;
10
+ billable: BillableOperator;
11
+ calls: CallsOperator;
12
+ notifications: NotificationsOperator;
8
13
  users: UsersOperator;
9
14
  tmp_usernames: TmpUsernamesOperator;
10
15
  tmp_phones: TmpPhonesOperator;
@@ -15,11 +20,15 @@ export type DefaultFXOptions = {
15
20
  auth: AuthOperator;
16
21
  };
17
22
  export interface FXOperator {
23
+ billable: BillableFXOperator;
24
+ calls: CallsFXOperator;
18
25
  users: UsersFXOperator;
19
26
  session: SessionFXOperator;
20
27
  preferences: PreferencesFXOperator;
21
28
  }
22
29
  export declare class FX implements FXOperator {
30
+ billable: BillableFX;
31
+ calls: CallsFX;
23
32
  users: UsersFX;
24
33
  session: SessionFX;
25
34
  preferences: PreferencesFX;
package/dist/src/fx.js CHANGED
@@ -3,15 +3,26 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
4
  // not use this file except in compliance with the License. You may obtain
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- import { newHTTPClient, newPreferencesClient, newTmpPhonesClient, newTmpUsernamesClient, newUsersClient, } from "@fabriktor/client";
6
+ import { newBillableClient, newCallsClient, newHTTPClient, newNotificationsClient, newPreferencesClient, newTmpPhonesClient, newTmpUsernamesClient, newUsersClient, } from "@fabriktor/client";
7
+ import { newBillableFX } from "./billable/billable.js";
8
+ import { newCallsFX } from "./calls/calls.js";
7
9
  import { newPreferencesFX } from "./preferences/preferences.js";
8
10
  import { newSessionFX } from "./session/session.js";
9
11
  import { newUsersFX } from "./users/users.js";
10
12
  export class FX {
13
+ billable;
14
+ calls;
11
15
  users;
12
16
  session;
13
17
  preferences;
14
18
  constructor(opts) {
19
+ this.billable = newBillableFX({
20
+ billable: opts.billable,
21
+ });
22
+ this.calls = newCallsFX({
23
+ calls: opts.calls,
24
+ notifications: opts.notifications,
25
+ });
15
26
  this.users = newUsersFX({
16
27
  users: opts.users,
17
28
  tmp_usernames: opts.tmp_usernames,
@@ -22,7 +33,8 @@ export class FX {
22
33
  });
23
34
  this.session = newSessionFX({
24
35
  auth: opts.auth,
25
- users: this.users,
36
+ users: opts.users,
37
+ users_fx: this.users,
26
38
  });
27
39
  }
28
40
  }
@@ -34,6 +46,21 @@ export function newDefaultFX(opts) {
34
46
  const get_token = () => opts.auth.getToken();
35
47
  return newFX({
36
48
  auth: opts.auth,
49
+ billable: newBillableClient({
50
+ http,
51
+ base_url: opts.base_url,
52
+ get_token,
53
+ }),
54
+ calls: newCallsClient({
55
+ http,
56
+ base_url: opts.base_url,
57
+ get_token,
58
+ }),
59
+ notifications: newNotificationsClient({
60
+ http,
61
+ base_url: opts.base_url,
62
+ get_token,
63
+ }),
37
64
  users: newUsersClient({
38
65
  http,
39
66
  base_url: opts.base_url,
@@ -1,4 +1,6 @@
1
1
  export * from "./auth/auth.js";
2
+ export * from "./billable/billable.js";
3
+ export * from "./calls/calls.js";
2
4
  export * from "./core/err.js";
3
5
  export * from "./fx.js";
4
6
  export * from "./preferences/preferences.js";
package/dist/src/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./auth/auth.js";
2
+ export * from "./billable/billable.js";
3
+ export * from "./calls/calls.js";
2
4
  export * from "./core/err.js";
3
5
  export * from "./fx.js";
4
6
  export * from "./preferences/preferences.js";
@@ -1,5 +1,5 @@
1
- import { type InPrefLanguage, type OperationResult, type PrefLanguage } from "@fabriktor/schema";
2
- import type { InsertOneOptionsCRUD, OperationResultOf, PreferencesOperator, QueryOptionsCRUD, ReplaceOneOptionsCRUD } from "@fabriktor/client";
1
+ import type { OperationResult } from "@fabriktor/schema";
2
+ import type { PreferencesOperator } from "@fabriktor/client";
3
3
  export type EnsurePrefLanguageOptions = {
4
4
  owner_id: string;
5
5
  language: string;
@@ -7,9 +7,6 @@ export type EnsurePrefLanguageOptions = {
7
7
  speech_to_text_source_language?: string;
8
8
  };
9
9
  export interface PreferencesFXOperator {
10
- queryPrefLanguages(opts?: QueryOptionsCRUD): Promise<OperationResultOf<PrefLanguage[]>>;
11
- insertOnePrefLanguage(opts: InsertOneOptionsCRUD<InPrefLanguage>): Promise<OperationResult>;
12
- replaceOnePrefLanguage(opts: ReplaceOneOptionsCRUD<InPrefLanguage>): Promise<OperationResult>;
13
10
  ensurePrefLanguage(opts: EnsurePrefLanguageOptions): Promise<OperationResult>;
14
11
  }
15
12
  export type PreferencesFXOptions = {
@@ -18,9 +15,6 @@ export type PreferencesFXOptions = {
18
15
  export declare class PreferencesFX implements PreferencesFXOperator {
19
16
  private preferences;
20
17
  constructor(opts: PreferencesFXOptions);
21
- queryPrefLanguages(opts?: QueryOptionsCRUD): Promise<OperationResultOf<PrefLanguage[]>>;
22
- insertOnePrefLanguage(opts: InsertOneOptionsCRUD<InPrefLanguage>): Promise<OperationResult>;
23
- replaceOnePrefLanguage(opts: ReplaceOneOptionsCRUD<InPrefLanguage>): Promise<OperationResult>;
24
18
  ensurePrefLanguage(opts: EnsurePrefLanguageOptions): Promise<OperationResult>;
25
19
  }
26
20
  export declare function newPreferencesFX(opts: PreferencesFXOptions): PreferencesFX;
@@ -3,28 +3,17 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
4
  // not use this file except in compliance with the License. You may obtain
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- import { OperationResultValueKey, } from "@fabriktor/schema";
6
+ import { queryParam } from "@fabriktor/client";
7
7
  export class PreferencesFX {
8
8
  preferences;
9
9
  constructor(opts) {
10
10
  this.preferences = opts.preferences;
11
11
  }
12
- async queryPrefLanguages(opts) {
13
- return await this.preferences.queryPrefLanguages(opts);
14
- }
15
- async insertOnePrefLanguage(opts) {
16
- return await this.preferences.insertOnePrefLanguage(opts);
17
- }
18
- async replaceOnePrefLanguage(opts) {
19
- return await this.preferences.replaceOnePrefLanguage(opts);
20
- }
21
12
  async ensurePrefLanguage(opts) {
22
13
  const out = await this.preferences.queryPrefLanguages({
23
- query: {
24
- owner_id: opts.owner_id,
25
- },
14
+ query: queryParam("owner_id", opts.owner_id),
26
15
  });
27
- const prefs = out[OperationResultValueKey] ?? [];
16
+ const prefs = out.value ?? [];
28
17
  const pref = prefs[0];
29
18
  const input = {
30
19
  is_default: false,
@@ -1,7 +1,7 @@
1
- import type { Op } from "@fabriktor/client";
2
1
  import type { InUser, PLUsersForgotPassword, PLUsersSignInLink, PLUsersVerifyEmail, UpsertResult } from "@fabriktor/schema";
2
+ import type { BootstrapOptions, OperationResultOf, UsersOperator } from "@fabriktor/client";
3
3
  import type { AuthEmailOptions, AuthOperator } from "../auth/auth.js";
4
- import type { BootstrapOptions, UsersFXOperator } from "../users/users.js";
4
+ import type { UsersFXOperator } from "../users/users.js";
5
5
  export type SignInMode = "email" | "username";
6
6
  export type SignInOptions = {
7
7
  mode: SignInMode;
@@ -13,12 +13,13 @@ export type SignUpAndBootstrapOptions = AuthEmailOptions & {
13
13
  };
14
14
  export type SessionFXOptions = {
15
15
  auth: AuthOperator;
16
- users: UsersFXOperator;
16
+ users: UsersOperator;
17
+ users_fx: UsersFXOperator;
17
18
  };
18
19
  export interface SessionFXOperator {
19
20
  startSignUp(opts: AuthEmailOptions): Promise<void>;
20
- finishSignUp(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
21
- signUpAndBootstrap(opts: SignUpAndBootstrapOptions): Promise<Op<UpsertResult>>;
21
+ finishSignUp(opts: BootstrapOptions): Promise<OperationResultOf<UpsertResult>>;
22
+ signUpAndBootstrap(opts: SignUpAndBootstrapOptions): Promise<OperationResultOf<UpsertResult>>;
22
23
  signIn(opts: SignInOptions): Promise<void>;
23
24
  signInWithEmail(opts: AuthEmailOptions): Promise<void>;
24
25
  signOut(): Promise<void>;
@@ -30,10 +31,11 @@ export interface SessionFXOperator {
30
31
  export declare class SessionFX implements SessionFXOperator {
31
32
  private auth;
32
33
  private users;
34
+ private users_fx;
33
35
  constructor(opts: SessionFXOptions);
34
36
  startSignUp(opts: AuthEmailOptions): Promise<void>;
35
- finishSignUp(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
36
- signUpAndBootstrap(opts: SignUpAndBootstrapOptions): Promise<Op<UpsertResult>>;
37
+ finishSignUp(opts: BootstrapOptions): Promise<OperationResultOf<UpsertResult>>;
38
+ signUpAndBootstrap(opts: SignUpAndBootstrapOptions): Promise<OperationResultOf<UpsertResult>>;
37
39
  signIn(opts: SignInOptions): Promise<void>;
38
40
  signInWithEmail(opts: AuthEmailOptions): Promise<void>;
39
41
  signOut(): Promise<void>;
@@ -6,9 +6,11 @@
6
6
  export class SessionFX {
7
7
  auth;
8
8
  users;
9
+ users_fx;
9
10
  constructor(opts) {
10
11
  this.auth = opts.auth;
11
12
  this.users = opts.users;
13
+ this.users_fx = opts.users_fx;
12
14
  }
13
15
  async startSignUp(opts) {
14
16
  await this.auth.signUp({
@@ -33,7 +35,7 @@ export class SessionFX {
33
35
  async signIn(opts) {
34
36
  const email = opts.mode === "email"
35
37
  ? opts.identifier
36
- : await this.users.resolveUsername({
38
+ : await this.users_fx.resolveUsername({
37
39
  username: opts.identifier,
38
40
  });
39
41
  await this.signInWithEmail({
@@ -49,13 +51,19 @@ export class SessionFX {
49
51
  await this.auth.signOut();
50
52
  }
51
53
  async forgotPassword(opts) {
52
- await this.users.forgotPassword(opts);
54
+ await this.users.forgotPassword({
55
+ email: opts.email,
56
+ });
53
57
  }
54
58
  async sendSignInLink(opts) {
55
- await this.users.sendSignInLink(opts);
59
+ await this.users.sendSignInLink({
60
+ email: opts.email,
61
+ });
56
62
  }
57
63
  async sendEmailVerification(opts) {
58
- await this.users.sendEmailVerification(opts);
64
+ await this.users.sendEmailVerification({
65
+ language: opts.language,
66
+ });
59
67
  }
60
68
  async checkEmailVerification() {
61
69
  await this.auth.reloadUser();
@@ -1,45 +1,26 @@
1
- import type { InUser, OperationResult, PLUsersVerifyEmail, PLUsersContactList, PLUsersForgotPassword, PLUsersResolveUsername, PLUsersSendCode, PLUsersSignInLink, PLUsersUsername, PLUsersVerifyCode, UpsertResult, User } from "@fabriktor/schema";
2
- import type { Op, TmpPhonesOperator, TmpUsernamesOperator, UsernameVerification, UsersOperator, GetByUIDOptions, SearchOptions } from "@fabriktor/client";
1
+ import type { PLUsersResolveUsername, PLUsersSendCode, PLUsersUsername, PLUsersVerifyCode } from "@fabriktor/schema";
2
+ import type { OperationResultOf, TmpPhonesOperator, TmpUsernamesOperator, UsernameVerification, UsersOperator } from "@fabriktor/client";
3
3
  export type UsersFXOptions = {
4
4
  users: UsersOperator;
5
5
  tmp_usernames: TmpUsernamesOperator;
6
6
  tmp_phones: TmpPhonesOperator;
7
7
  };
8
- export type BootstrapOptions = {
9
- in: InUser;
10
- };
11
8
  export interface UsersFXOperator {
12
- bootstrap(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
13
- sync(): Promise<OperationResult>;
14
- sendEmailVerification(opts: PLUsersVerifyEmail): Promise<void>;
15
- forgotPassword(opts: PLUsersForgotPassword): Promise<void>;
16
- sendSignInLink(opts: PLUsersSignInLink): Promise<void>;
17
9
  resolveUsername(opts: PLUsersResolveUsername): Promise<string>;
18
- verifyUsername(opts: PLUsersUsername): Promise<Op<UsernameVerification>>;
19
- generateUsernames(opts: PLUsersUsername): Promise<Op<UsernameVerification>>;
10
+ verifyUsername(opts: PLUsersUsername): Promise<OperationResultOf<UsernameVerification>>;
11
+ generateUsernames(opts: PLUsersUsername): Promise<OperationResultOf<UsernameVerification>>;
20
12
  sendPhoneCode(opts: PLUsersSendCode): Promise<void>;
21
13
  verifyPhoneCode(opts: PLUsersVerifyCode): Promise<void>;
22
- match(opts: PLUsersContactList): Promise<Op<User[]>>;
23
- search(opts: SearchOptions): Promise<Op<User[]>>;
24
- getByUID(opts: GetByUIDOptions): Promise<Op<User[]>>;
25
14
  }
26
15
  export declare class UsersFX implements UsersFXOperator {
27
16
  private users;
28
17
  private tmp_usernames;
29
18
  private tmp_phones;
30
19
  constructor(opts: UsersFXOptions);
31
- bootstrap(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
32
- sync(): Promise<OperationResult>;
33
- sendEmailVerification(opts: PLUsersVerifyEmail): Promise<void>;
34
- forgotPassword(opts: PLUsersForgotPassword): Promise<void>;
35
- sendSignInLink(opts: PLUsersSignInLink): Promise<void>;
36
20
  resolveUsername(opts: PLUsersResolveUsername): Promise<string>;
37
- verifyUsername(opts: PLUsersUsername): Promise<Op<UsernameVerification>>;
38
- generateUsernames(opts: PLUsersUsername): Promise<Op<UsernameVerification>>;
21
+ verifyUsername(opts: PLUsersUsername): Promise<OperationResultOf<UsernameVerification>>;
22
+ generateUsernames(opts: PLUsersUsername): Promise<OperationResultOf<UsernameVerification>>;
39
23
  sendPhoneCode(opts: PLUsersSendCode): Promise<void>;
40
24
  verifyPhoneCode(opts: PLUsersVerifyCode): Promise<void>;
41
- match(opts: PLUsersContactList): Promise<Op<User[]>>;
42
- search(opts: SearchOptions): Promise<Op<User[]>>;
43
- getByUID(opts: GetByUIDOptions): Promise<Op<User[]>>;
44
25
  }
45
26
  export declare function newUsersFX(opts: UsersFXOptions): UsersFX;
@@ -15,58 +15,25 @@ export class UsersFX {
15
15
  this.tmp_usernames = opts.tmp_usernames;
16
16
  this.tmp_phones = opts.tmp_phones;
17
17
  }
18
- async bootstrap(opts) {
19
- return await this.users.bootstrap({
20
- in: opts.in,
21
- });
22
- }
23
- async sync() {
24
- return await this.users.sync();
25
- }
26
- async sendEmailVerification(opts) {
27
- return await this.users.sendEmailVerification(opts);
28
- }
29
- async forgotPassword(opts) {
30
- return await this.users.forgotPassword({
31
- email: opts.email,
32
- });
33
- }
34
- async sendSignInLink(opts) {
35
- return await this.users.sendSignInLink({
36
- email: opts.email,
37
- });
38
- }
39
- // public async resolveUsername(opts: PLUsersResolveUsername): Promise<string> {
40
- // const username = normalizeUsername(opts.username);
41
- // const out = await this.users.resolveUsername({
42
- // username,
43
- // });
44
- // if (out.value === undefined) {
45
- // throw errUsernameResolveFailed();
46
- // }
47
- // return out.value;
48
- // }
49
18
  async resolveUsername(opts) {
50
19
  const username = normalizeUsername(opts.username);
51
- console.log("[fx/users.resolveUsername] normalized", {
52
- input: opts.username,
53
- username,
54
- });
55
20
  const out = await this.users.resolveUsername({
56
21
  username,
57
22
  });
58
- console.log("[fx/users.resolveUsername] response", out);
59
23
  if (out.value === undefined ||
60
24
  out.value.trim() === "" ||
61
25
  !out.value.includes("@")) {
62
- throw errUsernameResolveFailed();
26
+ throw errUsernameResolveFailed("username");
63
27
  }
64
28
  return out.value;
65
29
  }
66
30
  async verifyUsername(opts) {
67
31
  const check = validateUsernameFormat(opts.to_validate);
68
32
  if (!check.valid) {
69
- throw errUsernameInvalid(check.error);
33
+ throw errUsernameInvalid({
34
+ field: "to_validate",
35
+ msg: check.error,
36
+ });
70
37
  }
71
38
  return await this.tmp_usernames.verify({
72
39
  words: opts.words,
@@ -82,7 +49,7 @@ export class UsersFX {
82
49
  async sendPhoneCode(opts) {
83
50
  const phone = normalizePhone(opts.phone);
84
51
  if (!validatePhone(phone)) {
85
- throw errPhoneRequired();
52
+ throw errPhoneRequired("phone");
86
53
  }
87
54
  return await this.tmp_phones.send({
88
55
  phone,
@@ -92,32 +59,16 @@ export class UsersFX {
92
59
  const phone = normalizePhone(opts.phone);
93
60
  const code = normalizePhoneCode(opts.code);
94
61
  if (!validatePhone(phone)) {
95
- throw errPhoneRequired();
62
+ throw errPhoneRequired("phone");
96
63
  }
97
64
  if (!validatePhoneCode(code)) {
98
- throw errPhoneCodeRequired();
65
+ throw errPhoneCodeRequired("code");
99
66
  }
100
67
  return await this.tmp_phones.verify({
101
68
  phone,
102
69
  code,
103
70
  });
104
71
  }
105
- async match(opts) {
106
- return await this.users.match({
107
- contacts: opts.contacts,
108
- });
109
- }
110
- async search(opts) {
111
- return await this.users.search({
112
- q: opts.q,
113
- limit: opts.limit,
114
- });
115
- }
116
- async getByUID(opts) {
117
- return await this.users.getByUID({
118
- uid: opts.uid,
119
- });
120
- }
121
72
  }
122
73
  export function newUsersFX(opts) {
123
74
  return new UsersFX(opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.30",
3
+ "version": "0.0.31",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "^0.0.25",
21
- "@fabriktor/schema": "^0.0.18"
20
+ "@fabriktor/client": "0.0.32",
21
+ "@fabriktor/schema": "0.0.27"
22
22
  }
23
23
  }