@findatruck/aggregator-client 1.1.0

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.
package/dist/index.cjs ADDED
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AggregatorClient: () => AggregatorClient,
24
+ AggregatorClientError: () => AggregatorClientError,
25
+ NewLoginRequestSchema: () => import_shared_schemas2.NewLoginRequestSchema,
26
+ NewLoginResponseFailureSchema: () => import_shared_schemas2.NewLoginResponseFailureSchema,
27
+ NewLoginResponseSchema: () => import_shared_schemas2.NewLoginResponseSchema,
28
+ NewLoginResponseSuccessSchema: () => import_shared_schemas2.NewLoginResponseSuccessSchema,
29
+ ValidatePasswordRequestSchema: () => import_shared_schemas2.ValidatePasswordRequestSchema,
30
+ ValidatePasswordResponseSchema: () => import_shared_schemas2.ValidatePasswordResponseSchema,
31
+ endpoints: () => endpoints
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+
35
+ // src/endpoints.ts
36
+ var import_shared_schemas = require("@findatruck/shared-schemas");
37
+ var endpoints = {
38
+ newLogin: {
39
+ path: "/provider-account/new-login",
40
+ method: "POST",
41
+ requestSchema: import_shared_schemas.NewLoginRequestSchema,
42
+ responseSchema: import_shared_schemas.NewLoginResponseSchema
43
+ },
44
+ validatePassword: {
45
+ path: "/provider-account/validate-password",
46
+ method: "POST",
47
+ requestSchema: import_shared_schemas.ValidatePasswordRequestSchema,
48
+ responseSchema: import_shared_schemas.ValidatePasswordResponseSchema
49
+ }
50
+ };
51
+
52
+ // src/types.ts
53
+ var AggregatorClientError = class extends Error {
54
+ constructor(message, endpoint, status, responseBody) {
55
+ super(message);
56
+ this.endpoint = endpoint;
57
+ this.status = status;
58
+ this.responseBody = responseBody;
59
+ this.name = "AggregatorClientError";
60
+ }
61
+ };
62
+
63
+ // src/client.ts
64
+ var AggregatorClient = class {
65
+ constructor(config) {
66
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
67
+ this.workerSecret = config.workerSecret;
68
+ }
69
+ /**
70
+ * Initiates a new login for a provider account.
71
+ * This triggers the aggregator to scrape the provider and sync driver data.
72
+ */
73
+ async newLogin(request) {
74
+ const endpoint = endpoints.newLogin;
75
+ const parseResult = endpoint.requestSchema.safeParse(request);
76
+ if (!parseResult.success) {
77
+ throw new AggregatorClientError(
78
+ `Invalid request: ${parseResult.error.message}`,
79
+ endpoint.path
80
+ );
81
+ }
82
+ const response = await this.makeRequest(endpoint.path, endpoint.method, request);
83
+ const responseParseResult = endpoint.responseSchema.safeParse(response);
84
+ if (!responseParseResult.success) {
85
+ throw new AggregatorClientError(
86
+ `Invalid response from aggregator: ${responseParseResult.error.message}`,
87
+ endpoint.path,
88
+ void 0,
89
+ response
90
+ );
91
+ }
92
+ return responseParseResult.data;
93
+ }
94
+ /**
95
+ * Validates a password for an existing provider account.
96
+ * Returns whether the password is valid and the associated driver IDs.
97
+ */
98
+ async validatePassword(request) {
99
+ const endpoint = endpoints.validatePassword;
100
+ const parseResult = endpoint.requestSchema.safeParse(request);
101
+ if (!parseResult.success) {
102
+ throw new AggregatorClientError(
103
+ `Invalid request: ${parseResult.error.message}`,
104
+ endpoint.path
105
+ );
106
+ }
107
+ const response = await this.makeRequest(endpoint.path, endpoint.method, request);
108
+ const responseParseResult = endpoint.responseSchema.safeParse(response);
109
+ if (!responseParseResult.success) {
110
+ throw new AggregatorClientError(
111
+ `Invalid response from aggregator: ${responseParseResult.error.message}`,
112
+ endpoint.path,
113
+ void 0,
114
+ response
115
+ );
116
+ }
117
+ return responseParseResult.data;
118
+ }
119
+ async makeRequest(path, method, body) {
120
+ const url = `${this.baseUrl}${path}`;
121
+ const response = await fetch(url, {
122
+ method,
123
+ headers: {
124
+ "Content-Type": "application/json",
125
+ "Authorization": `Bearer ${this.workerSecret}`
126
+ },
127
+ body: JSON.stringify(body)
128
+ });
129
+ if (!response.ok) {
130
+ let responseBody;
131
+ try {
132
+ responseBody = await response.json();
133
+ } catch {
134
+ responseBody = await response.text();
135
+ }
136
+ throw new AggregatorClientError(
137
+ `Request to ${path} failed with status ${response.status}`,
138
+ path,
139
+ response.status,
140
+ responseBody
141
+ );
142
+ }
143
+ return response.json();
144
+ }
145
+ };
146
+
147
+ // src/index.ts
148
+ var import_shared_schemas2 = require("@findatruck/shared-schemas");
149
+ // Annotate the CommonJS export names for ESM import in node:
150
+ 0 && (module.exports = {
151
+ AggregatorClient,
152
+ AggregatorClientError,
153
+ NewLoginRequestSchema,
154
+ NewLoginResponseFailureSchema,
155
+ NewLoginResponseSchema,
156
+ NewLoginResponseSuccessSchema,
157
+ ValidatePasswordRequestSchema,
158
+ ValidatePasswordResponseSchema,
159
+ endpoints
160
+ });
@@ -0,0 +1,122 @@
1
+ import { NewLoginRequest, NewLoginResponse, ValidatePasswordRequest, ValidatePasswordResponse } from '@findatruck/shared-schemas';
2
+ export { NewLoginRequest, NewLoginRequestSchema, NewLoginResponse, NewLoginResponseFailure, NewLoginResponseFailureSchema, NewLoginResponseSchema, NewLoginResponseSuccess, NewLoginResponseSuccessSchema, ValidatePasswordRequest, ValidatePasswordRequestSchema, ValidatePasswordResponse, ValidatePasswordResponseSchema } from '@findatruck/shared-schemas';
3
+ import { z } from 'zod';
4
+
5
+ /**
6
+ * Configuration for the AggregatorClient
7
+ */
8
+ interface AggregatorClientConfig {
9
+ /** Base URL of the aggregator service (e.g., "https://aggregator.example.com") */
10
+ baseUrl: string;
11
+ /** Worker secret for authentication */
12
+ workerSecret: string;
13
+ }
14
+ /**
15
+ * Error thrown by the AggregatorClient when requests fail
16
+ */
17
+ declare class AggregatorClientError extends Error {
18
+ readonly endpoint: string;
19
+ readonly status?: number | undefined;
20
+ readonly responseBody?: unknown | undefined;
21
+ constructor(message: string, endpoint: string, status?: number | undefined, responseBody?: unknown | undefined);
22
+ }
23
+
24
+ /**
25
+ * Typed HTTP client for calling Aggregator endpoints from Convex actions.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const client = new AggregatorClient({
30
+ * baseUrl: process.env.AGGREGATOR_URL,
31
+ * workerSecret: process.env.WORKER_SECRET,
32
+ * });
33
+ *
34
+ * const result = await client.newLogin({
35
+ * providerUrl: 'https://provider.example.com',
36
+ * username: 'user@example.com',
37
+ * encryptedPassword: '...',
38
+ * ownerId: '...',
39
+ * externalProviderAccountId: '...',
40
+ * convexUpdateUrl: '...',
41
+ * });
42
+ * ```
43
+ */
44
+ declare class AggregatorClient {
45
+ private readonly baseUrl;
46
+ private readonly workerSecret;
47
+ constructor(config: AggregatorClientConfig);
48
+ /**
49
+ * Initiates a new login for a provider account.
50
+ * This triggers the aggregator to scrape the provider and sync driver data.
51
+ */
52
+ newLogin(request: NewLoginRequest): Promise<NewLoginResponse>;
53
+ /**
54
+ * Validates a password for an existing provider account.
55
+ * Returns whether the password is valid and the associated driver IDs.
56
+ */
57
+ validatePassword(request: ValidatePasswordRequest): Promise<ValidatePasswordResponse>;
58
+ private makeRequest;
59
+ }
60
+
61
+ /**
62
+ * Endpoint definition with path, method, and validation schemas
63
+ */
64
+ interface EndpointDefinition<TRequest extends z.ZodType, TResponse extends z.ZodType> {
65
+ path: string;
66
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
67
+ requestSchema: TRequest;
68
+ responseSchema: TResponse;
69
+ }
70
+ /**
71
+ * Single source of truth for all Aggregator HTTP endpoints
72
+ * consumed from Convex.
73
+ */
74
+ declare const endpoints: {
75
+ readonly newLogin: {
76
+ readonly path: "/provider-account/new-login";
77
+ readonly method: "POST";
78
+ readonly requestSchema: z.ZodObject<{
79
+ providerUrl: z.ZodString;
80
+ username: z.ZodString;
81
+ encryptedPassword: z.ZodString;
82
+ ownerId: z.ZodString;
83
+ externalProviderAccountId: z.ZodString;
84
+ convexUpdateUrl: z.ZodString;
85
+ }, z.core.$strip>;
86
+ readonly responseSchema: z.ZodUnion<readonly [z.ZodObject<{
87
+ success: z.ZodOptional<z.ZodBoolean>;
88
+ message: z.ZodString;
89
+ user: z.ZodObject<{
90
+ id: z.ZodString;
91
+ username: z.ZodString;
92
+ created_at: z.ZodCoercedDate<unknown>;
93
+ updated_at: z.ZodCoercedDate<unknown>;
94
+ }, z.core.$strip>;
95
+ provider: z.ZodObject<{
96
+ name: z.ZodString;
97
+ url: z.ZodString;
98
+ }, z.core.$strip>;
99
+ driverIds: z.ZodArray<z.ZodString>;
100
+ organizationId: z.ZodOptional<z.ZodString>;
101
+ honkUserId: z.ZodOptional<z.ZodString>;
102
+ }, z.core.$strip>, z.ZodObject<{
103
+ success: z.ZodOptional<z.ZodBoolean>;
104
+ error: z.ZodString;
105
+ }, z.core.$strip>]>;
106
+ };
107
+ readonly validatePassword: {
108
+ readonly path: "/provider-account/validate-password";
109
+ readonly method: "POST";
110
+ readonly requestSchema: z.ZodObject<{
111
+ providerAccountId: z.ZodString;
112
+ encryptedPassword: z.ZodString;
113
+ }, z.core.$strip>;
114
+ readonly responseSchema: z.ZodObject<{
115
+ isValid: z.ZodBoolean;
116
+ driverIds: z.ZodArray<z.ZodString>;
117
+ }, z.core.$strip>;
118
+ };
119
+ };
120
+ type EndpointName = keyof typeof endpoints;
121
+
122
+ export { AggregatorClient, type AggregatorClientConfig, AggregatorClientError, type EndpointDefinition, type EndpointName, endpoints };
@@ -0,0 +1,122 @@
1
+ import { NewLoginRequest, NewLoginResponse, ValidatePasswordRequest, ValidatePasswordResponse } from '@findatruck/shared-schemas';
2
+ export { NewLoginRequest, NewLoginRequestSchema, NewLoginResponse, NewLoginResponseFailure, NewLoginResponseFailureSchema, NewLoginResponseSchema, NewLoginResponseSuccess, NewLoginResponseSuccessSchema, ValidatePasswordRequest, ValidatePasswordRequestSchema, ValidatePasswordResponse, ValidatePasswordResponseSchema } from '@findatruck/shared-schemas';
3
+ import { z } from 'zod';
4
+
5
+ /**
6
+ * Configuration for the AggregatorClient
7
+ */
8
+ interface AggregatorClientConfig {
9
+ /** Base URL of the aggregator service (e.g., "https://aggregator.example.com") */
10
+ baseUrl: string;
11
+ /** Worker secret for authentication */
12
+ workerSecret: string;
13
+ }
14
+ /**
15
+ * Error thrown by the AggregatorClient when requests fail
16
+ */
17
+ declare class AggregatorClientError extends Error {
18
+ readonly endpoint: string;
19
+ readonly status?: number | undefined;
20
+ readonly responseBody?: unknown | undefined;
21
+ constructor(message: string, endpoint: string, status?: number | undefined, responseBody?: unknown | undefined);
22
+ }
23
+
24
+ /**
25
+ * Typed HTTP client for calling Aggregator endpoints from Convex actions.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const client = new AggregatorClient({
30
+ * baseUrl: process.env.AGGREGATOR_URL,
31
+ * workerSecret: process.env.WORKER_SECRET,
32
+ * });
33
+ *
34
+ * const result = await client.newLogin({
35
+ * providerUrl: 'https://provider.example.com',
36
+ * username: 'user@example.com',
37
+ * encryptedPassword: '...',
38
+ * ownerId: '...',
39
+ * externalProviderAccountId: '...',
40
+ * convexUpdateUrl: '...',
41
+ * });
42
+ * ```
43
+ */
44
+ declare class AggregatorClient {
45
+ private readonly baseUrl;
46
+ private readonly workerSecret;
47
+ constructor(config: AggregatorClientConfig);
48
+ /**
49
+ * Initiates a new login for a provider account.
50
+ * This triggers the aggregator to scrape the provider and sync driver data.
51
+ */
52
+ newLogin(request: NewLoginRequest): Promise<NewLoginResponse>;
53
+ /**
54
+ * Validates a password for an existing provider account.
55
+ * Returns whether the password is valid and the associated driver IDs.
56
+ */
57
+ validatePassword(request: ValidatePasswordRequest): Promise<ValidatePasswordResponse>;
58
+ private makeRequest;
59
+ }
60
+
61
+ /**
62
+ * Endpoint definition with path, method, and validation schemas
63
+ */
64
+ interface EndpointDefinition<TRequest extends z.ZodType, TResponse extends z.ZodType> {
65
+ path: string;
66
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
67
+ requestSchema: TRequest;
68
+ responseSchema: TResponse;
69
+ }
70
+ /**
71
+ * Single source of truth for all Aggregator HTTP endpoints
72
+ * consumed from Convex.
73
+ */
74
+ declare const endpoints: {
75
+ readonly newLogin: {
76
+ readonly path: "/provider-account/new-login";
77
+ readonly method: "POST";
78
+ readonly requestSchema: z.ZodObject<{
79
+ providerUrl: z.ZodString;
80
+ username: z.ZodString;
81
+ encryptedPassword: z.ZodString;
82
+ ownerId: z.ZodString;
83
+ externalProviderAccountId: z.ZodString;
84
+ convexUpdateUrl: z.ZodString;
85
+ }, z.core.$strip>;
86
+ readonly responseSchema: z.ZodUnion<readonly [z.ZodObject<{
87
+ success: z.ZodOptional<z.ZodBoolean>;
88
+ message: z.ZodString;
89
+ user: z.ZodObject<{
90
+ id: z.ZodString;
91
+ username: z.ZodString;
92
+ created_at: z.ZodCoercedDate<unknown>;
93
+ updated_at: z.ZodCoercedDate<unknown>;
94
+ }, z.core.$strip>;
95
+ provider: z.ZodObject<{
96
+ name: z.ZodString;
97
+ url: z.ZodString;
98
+ }, z.core.$strip>;
99
+ driverIds: z.ZodArray<z.ZodString>;
100
+ organizationId: z.ZodOptional<z.ZodString>;
101
+ honkUserId: z.ZodOptional<z.ZodString>;
102
+ }, z.core.$strip>, z.ZodObject<{
103
+ success: z.ZodOptional<z.ZodBoolean>;
104
+ error: z.ZodString;
105
+ }, z.core.$strip>]>;
106
+ };
107
+ readonly validatePassword: {
108
+ readonly path: "/provider-account/validate-password";
109
+ readonly method: "POST";
110
+ readonly requestSchema: z.ZodObject<{
111
+ providerAccountId: z.ZodString;
112
+ encryptedPassword: z.ZodString;
113
+ }, z.core.$strip>;
114
+ readonly responseSchema: z.ZodObject<{
115
+ isValid: z.ZodBoolean;
116
+ driverIds: z.ZodArray<z.ZodString>;
117
+ }, z.core.$strip>;
118
+ };
119
+ };
120
+ type EndpointName = keyof typeof endpoints;
121
+
122
+ export { AggregatorClient, type AggregatorClientConfig, AggregatorClientError, type EndpointDefinition, type EndpointName, endpoints };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ // src/endpoints.ts
2
+ import {
3
+ NewLoginRequestSchema,
4
+ NewLoginResponseSchema,
5
+ ValidatePasswordRequestSchema,
6
+ ValidatePasswordResponseSchema
7
+ } from "@findatruck/shared-schemas";
8
+ var endpoints = {
9
+ newLogin: {
10
+ path: "/provider-account/new-login",
11
+ method: "POST",
12
+ requestSchema: NewLoginRequestSchema,
13
+ responseSchema: NewLoginResponseSchema
14
+ },
15
+ validatePassword: {
16
+ path: "/provider-account/validate-password",
17
+ method: "POST",
18
+ requestSchema: ValidatePasswordRequestSchema,
19
+ responseSchema: ValidatePasswordResponseSchema
20
+ }
21
+ };
22
+
23
+ // src/types.ts
24
+ var AggregatorClientError = class extends Error {
25
+ constructor(message, endpoint, status, responseBody) {
26
+ super(message);
27
+ this.endpoint = endpoint;
28
+ this.status = status;
29
+ this.responseBody = responseBody;
30
+ this.name = "AggregatorClientError";
31
+ }
32
+ };
33
+
34
+ // src/client.ts
35
+ var AggregatorClient = class {
36
+ constructor(config) {
37
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
38
+ this.workerSecret = config.workerSecret;
39
+ }
40
+ /**
41
+ * Initiates a new login for a provider account.
42
+ * This triggers the aggregator to scrape the provider and sync driver data.
43
+ */
44
+ async newLogin(request) {
45
+ const endpoint = endpoints.newLogin;
46
+ const parseResult = endpoint.requestSchema.safeParse(request);
47
+ if (!parseResult.success) {
48
+ throw new AggregatorClientError(
49
+ `Invalid request: ${parseResult.error.message}`,
50
+ endpoint.path
51
+ );
52
+ }
53
+ const response = await this.makeRequest(endpoint.path, endpoint.method, request);
54
+ const responseParseResult = endpoint.responseSchema.safeParse(response);
55
+ if (!responseParseResult.success) {
56
+ throw new AggregatorClientError(
57
+ `Invalid response from aggregator: ${responseParseResult.error.message}`,
58
+ endpoint.path,
59
+ void 0,
60
+ response
61
+ );
62
+ }
63
+ return responseParseResult.data;
64
+ }
65
+ /**
66
+ * Validates a password for an existing provider account.
67
+ * Returns whether the password is valid and the associated driver IDs.
68
+ */
69
+ async validatePassword(request) {
70
+ const endpoint = endpoints.validatePassword;
71
+ const parseResult = endpoint.requestSchema.safeParse(request);
72
+ if (!parseResult.success) {
73
+ throw new AggregatorClientError(
74
+ `Invalid request: ${parseResult.error.message}`,
75
+ endpoint.path
76
+ );
77
+ }
78
+ const response = await this.makeRequest(endpoint.path, endpoint.method, request);
79
+ const responseParseResult = endpoint.responseSchema.safeParse(response);
80
+ if (!responseParseResult.success) {
81
+ throw new AggregatorClientError(
82
+ `Invalid response from aggregator: ${responseParseResult.error.message}`,
83
+ endpoint.path,
84
+ void 0,
85
+ response
86
+ );
87
+ }
88
+ return responseParseResult.data;
89
+ }
90
+ async makeRequest(path, method, body) {
91
+ const url = `${this.baseUrl}${path}`;
92
+ const response = await fetch(url, {
93
+ method,
94
+ headers: {
95
+ "Content-Type": "application/json",
96
+ "Authorization": `Bearer ${this.workerSecret}`
97
+ },
98
+ body: JSON.stringify(body)
99
+ });
100
+ if (!response.ok) {
101
+ let responseBody;
102
+ try {
103
+ responseBody = await response.json();
104
+ } catch {
105
+ responseBody = await response.text();
106
+ }
107
+ throw new AggregatorClientError(
108
+ `Request to ${path} failed with status ${response.status}`,
109
+ path,
110
+ response.status,
111
+ responseBody
112
+ );
113
+ }
114
+ return response.json();
115
+ }
116
+ };
117
+
118
+ // src/index.ts
119
+ import {
120
+ NewLoginRequestSchema as NewLoginRequestSchema2,
121
+ NewLoginResponseSchema as NewLoginResponseSchema2,
122
+ NewLoginResponseSuccessSchema,
123
+ NewLoginResponseFailureSchema,
124
+ ValidatePasswordRequestSchema as ValidatePasswordRequestSchema2,
125
+ ValidatePasswordResponseSchema as ValidatePasswordResponseSchema2
126
+ } from "@findatruck/shared-schemas";
127
+ export {
128
+ AggregatorClient,
129
+ AggregatorClientError,
130
+ NewLoginRequestSchema2 as NewLoginRequestSchema,
131
+ NewLoginResponseFailureSchema,
132
+ NewLoginResponseSchema2 as NewLoginResponseSchema,
133
+ NewLoginResponseSuccessSchema,
134
+ ValidatePasswordRequestSchema2 as ValidatePasswordRequestSchema,
135
+ ValidatePasswordResponseSchema2 as ValidatePasswordResponseSchema,
136
+ endpoints
137
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@findatruck/aggregator-client",
3
+ "version": "1.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "sideEffects": false,
19
+ "scripts": {
20
+ "build": "tsup src/index.ts --dts --format esm,cjs --out-dir dist --clean",
21
+ "prepublishOnly": "npm run build",
22
+ "test": "vitest run",
23
+ "test:watch": "vitest"
24
+ },
25
+ "dependencies": {
26
+ "@findatruck/shared-schemas": "^2.9.0"
27
+ },
28
+ "peerDependencies": {
29
+ "zod": "^4.1.11"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^24.7.0",
33
+ "tsup": "^8.0.0",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^3.2.4",
36
+ "zod": "^4.1.11"
37
+ }
38
+ }