@scrawn/core 0.0.2

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.
Files changed (77) hide show
  1. package/dist/config.d.ts +26 -0
  2. package/dist/config.d.ts.map +1 -0
  3. package/dist/config.js +25 -0
  4. package/dist/config.js.map +1 -0
  5. package/dist/core/auth/apiKeyAuth.d.ts +67 -0
  6. package/dist/core/auth/apiKeyAuth.d.ts.map +1 -0
  7. package/dist/core/auth/apiKeyAuth.js +71 -0
  8. package/dist/core/auth/apiKeyAuth.js.map +1 -0
  9. package/dist/core/auth/baseAuth.d.ts +92 -0
  10. package/dist/core/auth/baseAuth.d.ts.map +1 -0
  11. package/dist/core/auth/baseAuth.js +28 -0
  12. package/dist/core/auth/baseAuth.js.map +1 -0
  13. package/dist/core/grpc/client.d.ts +126 -0
  14. package/dist/core/grpc/client.d.ts.map +1 -0
  15. package/dist/core/grpc/client.js +138 -0
  16. package/dist/core/grpc/client.js.map +1 -0
  17. package/dist/core/grpc/index.d.ts +12 -0
  18. package/dist/core/grpc/index.d.ts.map +1 -0
  19. package/dist/core/grpc/index.js +11 -0
  20. package/dist/core/grpc/index.js.map +1 -0
  21. package/dist/core/grpc/requestBuilder.d.ts +116 -0
  22. package/dist/core/grpc/requestBuilder.d.ts.map +1 -0
  23. package/dist/core/grpc/requestBuilder.js +146 -0
  24. package/dist/core/grpc/requestBuilder.js.map +1 -0
  25. package/dist/core/grpc/types.d.ts +54 -0
  26. package/dist/core/grpc/types.d.ts.map +1 -0
  27. package/dist/core/grpc/types.js +9 -0
  28. package/dist/core/grpc/types.js.map +1 -0
  29. package/dist/core/scrawn.d.ts +197 -0
  30. package/dist/core/scrawn.d.ts.map +1 -0
  31. package/dist/core/scrawn.js +331 -0
  32. package/dist/core/scrawn.js.map +1 -0
  33. package/dist/core/types/auth.d.ts +31 -0
  34. package/dist/core/types/auth.d.ts.map +1 -0
  35. package/dist/core/types/auth.js +2 -0
  36. package/dist/core/types/auth.js.map +1 -0
  37. package/dist/core/types/event.d.ts +119 -0
  38. package/dist/core/types/event.d.ts.map +1 -0
  39. package/dist/core/types/event.js +15 -0
  40. package/dist/core/types/event.js.map +1 -0
  41. package/dist/gen/auth/v1/auth_connect.d.ts +22 -0
  42. package/dist/gen/auth/v1/auth_connect.d.ts.map +1 -0
  43. package/dist/gen/auth/v1/auth_connect.js +26 -0
  44. package/dist/gen/auth/v1/auth_connect.js.map +1 -0
  45. package/dist/gen/auth/v1/auth_pb.d.ts +59 -0
  46. package/dist/gen/auth/v1/auth_pb.d.ts.map +1 -0
  47. package/dist/gen/auth/v1/auth_pb.js +93 -0
  48. package/dist/gen/auth/v1/auth_pb.js.map +1 -0
  49. package/dist/gen/event/v1/event_connect.d.ts +22 -0
  50. package/dist/gen/event/v1/event_connect.d.ts.map +1 -0
  51. package/dist/gen/event/v1/event_connect.js +26 -0
  52. package/dist/gen/event/v1/event_connect.js.map +1 -0
  53. package/dist/gen/event/v1/event_pb.d.ts +105 -0
  54. package/dist/gen/event/v1/event_pb.d.ts.map +1 -0
  55. package/dist/gen/event/v1/event_pb.js +154 -0
  56. package/dist/gen/event/v1/event_pb.js.map +1 -0
  57. package/dist/gen/payment/v1/payment_connect.d.ts +22 -0
  58. package/dist/gen/payment/v1/payment_connect.d.ts.map +1 -0
  59. package/dist/gen/payment/v1/payment_connect.js +26 -0
  60. package/dist/gen/payment/v1/payment_connect.js.map +1 -0
  61. package/dist/gen/payment/v1/payment_pb.d.ts +37 -0
  62. package/dist/gen/payment/v1/payment_pb.d.ts.map +1 -0
  63. package/dist/gen/payment/v1/payment_pb.js +66 -0
  64. package/dist/gen/payment/v1/payment_pb.js.map +1 -0
  65. package/dist/index.d.ts +11 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +15 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/utils/logger.d.ts +10 -0
  70. package/dist/utils/logger.d.ts.map +1 -0
  71. package/dist/utils/logger.js +62 -0
  72. package/dist/utils/logger.js.map +1 -0
  73. package/dist/utils/pathMatcher.d.ts +25 -0
  74. package/dist/utils/pathMatcher.d.ts.map +1 -0
  75. package/dist/utils/pathMatcher.js +46 -0
  76. package/dist/utils/pathMatcher.js.map +1 -0
  77. package/package.json +37 -0
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Central configuration for the Scrawn SDK.
3
+ * All configuration values should be defined here for easy maintenance.
4
+ */
5
+ export declare const ScrawnConfig: {
6
+ /**
7
+ * gRPC client configuration
8
+ */
9
+ readonly grpc: {
10
+ /**
11
+ * HTTP version to use for gRPC transport.
12
+ */
13
+ readonly httpVersion: "1.1";
14
+ };
15
+ /**
16
+ * Logging configuration
17
+ */
18
+ readonly logging: {
19
+ /**
20
+ * Enable debug logs
21
+ */
22
+ readonly enableDebug: false;
23
+ };
24
+ };
25
+ export type ScrawnConfig = typeof ScrawnConfig;
26
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,eAAO,MAAM,YAAY;IACvB;;OAEG;;QAED;;WAEG;;;IAKL;;OAEG;;QAED;;WAEG;;;CAGG,CAAC;AAEX,MAAM,MAAM,YAAY,GAAG,OAAO,YAAY,CAAC"}
package/dist/config.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Central configuration for the Scrawn SDK.
3
+ * All configuration values should be defined here for easy maintenance.
4
+ */
5
+ export const ScrawnConfig = {
6
+ /**
7
+ * gRPC client configuration
8
+ */
9
+ grpc: {
10
+ /**
11
+ * HTTP version to use for gRPC transport.
12
+ */
13
+ httpVersion: '1.1',
14
+ },
15
+ /**
16
+ * Logging configuration
17
+ */
18
+ logging: {
19
+ /**
20
+ * Enable debug logs
21
+ */
22
+ enableDebug: false,
23
+ },
24
+ };
25
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,IAAI,EAAE;QACJ;;WAEG;QACH,WAAW,EAAE,KAAc;KAE5B;IAED;;OAEG;IACH,OAAO,EAAE;QACP;;WAEG;QACH,WAAW,EAAE,KAAK;KACnB;CACO,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { AuthBase } from './baseAuth.js';
2
+ import type { Scrawn } from '../scrawn.js';
3
+ /**
4
+ * API key format: sk_ followed by 16 alphanumeric characters
5
+ * @example 'sk_abc123def456ghi7'
6
+ */
7
+ export type ApiKeyFormat = `scrn_${string}`;
8
+ /**
9
+ * Type guard to validate API key format
10
+ */
11
+ export declare function isValidApiKey(key: string): key is ApiKeyFormat;
12
+ /**
13
+ * Validates and returns a properly typed API key
14
+ * @throws Error if the API key format is invalid
15
+ */
16
+ export declare function validateApiKey(key: string): ApiKeyFormat;
17
+ /**
18
+ * Credentials structure for API key authentication.
19
+ */
20
+ export type ApiKeyAuthCreds = {
21
+ apiKey: ApiKeyFormat;
22
+ };
23
+ /**
24
+ * Simple API key authentication method.
25
+ *
26
+ * Provides authentication using a static API key.
27
+ * This is the default authentication method registered by the SDK.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const auth = new ApiKeyAuth('sk_test_...');
32
+ * scrawn.registerAuthMethod('api', auth);
33
+ * ```
34
+ */
35
+ export declare class ApiKeyAuth extends AuthBase<ApiKeyAuthCreds> {
36
+ /** Authentication method identifier */
37
+ name: "api";
38
+ /** Validated API key */
39
+ private validatedKey;
40
+ /**
41
+ * Creates a new API key authentication instance.
42
+ *
43
+ * @param apiKey - Your Scrawn API key (format: sk_<16 alphanumeric chars>)
44
+ * @throws Error if API key format is invalid
45
+ */
46
+ constructor(apiKey: string);
47
+ /**
48
+ * Initialize the API key auth method.
49
+ * No additional setup is required for API key authentication.
50
+ *
51
+ * @param scrawn - The Scrawn SDK instance (optional, unused)
52
+ */
53
+ init(scrawn?: Scrawn): Promise<void>;
54
+ /**
55
+ * Get the API key credentials.
56
+ *
57
+ * @returns A promise that resolves to an object containing the API key
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const creds = await auth.getCreds();
62
+ * // { apiKey: 'sk_abc123def456ghi7' }
63
+ * ```
64
+ */
65
+ getCreds(): Promise<ApiKeyAuthCreds>;
66
+ }
67
+ //# sourceMappingURL=apiKeyAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apiKeyAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/apiKeyAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAK3C;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,MAAM,EAAE,CAAC;AAE5C;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,YAAY,CAE9D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAQxD;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,qBAAa,UAAW,SAAQ,QAAQ,CAAC,eAAe,CAAC;IACvD,uCAAuC;IACvC,IAAI,EAAG,KAAK,CAAU;IAEtB,wBAAwB;IACxB,OAAO,CAAC,YAAY,CAAe;IAEnC;;;;;OAKG;gBACS,MAAM,EAAE,MAAM;IAK1B;;;;;OAKG;IACG,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM;IAK1B;;;;;;;;;;OAUG;IACG,QAAQ,IAAI,OAAO,CAAC,eAAe,CAAC;CAG3C"}
@@ -0,0 +1,71 @@
1
+ import { AuthBase } from './baseAuth.js';
2
+ import { ScrawnLogger } from '../../utils/logger.js';
3
+ const log = new ScrawnLogger('Auth');
4
+ /**
5
+ * Type guard to validate API key format
6
+ */
7
+ export function isValidApiKey(key) {
8
+ return /^scrn_[a-zA-Z0-9]{32}$/.test(key);
9
+ }
10
+ /**
11
+ * Validates and returns a properly typed API key
12
+ * @throws Error if the API key format is invalid
13
+ */
14
+ export function validateApiKey(key) {
15
+ if (!isValidApiKey(key)) {
16
+ log.error(`Invalid API key format: "${key}".`);
17
+ throw new Error(`Invalid API key format. Expected format: sk_<16 alphanumeric characters>. Got: ${key.substring(0, 10)}...`);
18
+ }
19
+ return key;
20
+ }
21
+ /**
22
+ * Simple API key authentication method.
23
+ *
24
+ * Provides authentication using a static API key.
25
+ * This is the default authentication method registered by the SDK.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const auth = new ApiKeyAuth('sk_test_...');
30
+ * scrawn.registerAuthMethod('api', auth);
31
+ * ```
32
+ */
33
+ export class ApiKeyAuth extends AuthBase {
34
+ /**
35
+ * Creates a new API key authentication instance.
36
+ *
37
+ * @param apiKey - Your Scrawn API key (format: sk_<16 alphanumeric chars>)
38
+ * @throws Error if API key format is invalid
39
+ */
40
+ constructor(apiKey) {
41
+ super();
42
+ /** Authentication method identifier */
43
+ this.name = 'api';
44
+ this.validatedKey = validateApiKey(apiKey);
45
+ }
46
+ /**
47
+ * Initialize the API key auth method.
48
+ * No additional setup is required for API key authentication.
49
+ *
50
+ * @param scrawn - The Scrawn SDK instance (optional, unused)
51
+ */
52
+ async init(scrawn) {
53
+ // nothing extra for now
54
+ return;
55
+ }
56
+ /**
57
+ * Get the API key credentials.
58
+ *
59
+ * @returns A promise that resolves to an object containing the API key
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const creds = await auth.getCreds();
64
+ * // { apiKey: 'sk_abc123def456ghi7' }
65
+ * ```
66
+ */
67
+ async getCreds() {
68
+ return { apiKey: this.validatedKey };
69
+ }
70
+ }
71
+ //# sourceMappingURL=apiKeyAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apiKeyAuth.js","sourceRoot":"","sources":["../../../src/core/auth/apiKeyAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,MAAM,GAAG,GAAG,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;AAQrC;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,OAAO,wBAAwB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC5C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC;QACxB,GAAG,CAAC,KAAK,CAAC,4BAA4B,GAAG,IAAI,CAAC,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,kFAAkF,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAC5G,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AASD;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,UAAW,SAAQ,QAAyB;IAOvD;;;;;OAKG;IACH,YAAY,MAAc;QACxB,KAAK,EAAE,CAAC;QAbV,uCAAuC;QACvC,SAAI,GAAG,KAAc,CAAC;QAapB,IAAI,CAAC,YAAY,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,MAAe;QACxB,wBAAwB;QACxB,OAAO;IACT,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,QAAQ;QACZ,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC;IACvC,CAAC;CACF"}
@@ -0,0 +1,92 @@
1
+ import type { Scrawn } from '../scrawn.js';
2
+ /**
3
+ * Abstract base class for authentication methods.
4
+ *
5
+ * All authentication implementations must extend this class and implement the required methods.
6
+ * Auth methods are responsible for managing and providing credentials for API requests.
7
+ *
8
+ * @template TCreds - The type of credentials this auth method returns
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * type MyAuthCreds = { token: string };
13
+ *
14
+ * export class MyAuth extends AuthBase<MyAuthCreds> {
15
+ * name = 'my_auth';
16
+ *
17
+ * async init(scrawn: Scrawn) {
18
+ * // Setup logic here
19
+ * }
20
+ *
21
+ * async getCreds(): Promise<MyAuthCreds> {
22
+ * return { token: 'my_token' };
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ export declare abstract class AuthBase<TCreds = unknown> {
28
+ /**
29
+ * Unique identifier for this authentication method.
30
+ * Used to register and reference the auth method (e.g., 'api', 'oauth').
31
+ */
32
+ abstract name: string;
33
+ /**
34
+ * Initialize the authentication method.
35
+ *
36
+ * Called once during SDK setup. Use this to perform any necessary
37
+ * initialization like token validation or refresh.
38
+ *
39
+ * @param scrawn - The Scrawn SDK instance
40
+ * @returns A promise that resolves when initialization is complete
41
+ */
42
+ abstract init(scrawn: Scrawn): Promise<void>;
43
+ /**
44
+ * Retrieve the current credentials for this authentication method.
45
+ *
46
+ * This method is called whenever an event needs to authenticate.
47
+ * Credentials are cached by the SDK, so this is typically only called once.
48
+ *
49
+ * @returns A promise that resolves to the credentials object
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * async getCreds(): Promise<MyAuthCreds> {
54
+ * return { apiKey: this.apiKey };
55
+ * }
56
+ * ```
57
+ */
58
+ abstract getCreds(): Promise<TCreds>;
59
+ /**
60
+ * Optional hook that runs before each event is processed.
61
+ *
62
+ * Use this for operations that must happen before every request,
63
+ * such as token refresh checks or rate limiting.
64
+ *
65
+ * @returns A promise that resolves when the pre-run hook completes
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * async preRun() {
70
+ * await this.refreshTokenIfNeeded();
71
+ * }
72
+ * ```
73
+ */
74
+ preRun?(): Promise<void>;
75
+ /**
76
+ * Optional hook that runs after each event is processed.
77
+ *
78
+ * Use this for cleanup operations or logging that should happen
79
+ * after every request completes.
80
+ *
81
+ * @returns A promise that resolves when the post-run hook completes
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * async postRun() {
86
+ * await this.logRequestMetrics();
87
+ * }
88
+ * ```
89
+ */
90
+ postRun?(): Promise<void>;
91
+ }
92
+ //# sourceMappingURL=baseAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseAuth.d.ts","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,8BAAsB,QAAQ,CAAC,MAAM,GAAG,OAAO;IAC7C;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAE5C;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CAAC,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;IAEpC;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;IAExB;;;;;;;;;;;;;;QAcI;IACF,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAC5B"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Abstract base class for authentication methods.
3
+ *
4
+ * All authentication implementations must extend this class and implement the required methods.
5
+ * Auth methods are responsible for managing and providing credentials for API requests.
6
+ *
7
+ * @template TCreds - The type of credentials this auth method returns
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * type MyAuthCreds = { token: string };
12
+ *
13
+ * export class MyAuth extends AuthBase<MyAuthCreds> {
14
+ * name = 'my_auth';
15
+ *
16
+ * async init(scrawn: Scrawn) {
17
+ * // Setup logic here
18
+ * }
19
+ *
20
+ * async getCreds(): Promise<MyAuthCreds> {
21
+ * return { token: 'my_token' };
22
+ * }
23
+ * }
24
+ * ```
25
+ */
26
+ export class AuthBase {
27
+ }
28
+ //# sourceMappingURL=baseAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseAuth.js","sourceRoot":"","sources":["../../../src/core/auth/baseAuth.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAgB,QAAQ;CAoE7B"}
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Type-safe gRPC client abstraction layer.
3
+ *
4
+ * This module provides the main entry point for making gRPC calls with
5
+ * full compile-time type safety and a beautiful fluent API.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const client = new GrpcClient('https://api.scrawn.dev');
10
+ *
11
+ * const response = await client
12
+ * .newCall(EventService, 'registerEvent')
13
+ * .addHeader('Authorization', `Bearer ${apiKey}`)
14
+ * .addHeader('x-request-id', '123')
15
+ * .addPayload({
16
+ * type: EventType.SDK_CALL,
17
+ * userId: 'user_123',
18
+ * data: { case: 'sdkCall', value: new SDKCall({ sdkCallType: SDKCallType.RAW, debitAmount: 10 }) }
19
+ * })
20
+ * .request();
21
+ * ```
22
+ */
23
+ import type { Transport } from '@connectrpc/connect';
24
+ import type { ServiceType } from '@bufbuild/protobuf';
25
+ import { RequestBuilder } from './requestBuilder.js';
26
+ import type { ServiceMethodNames } from './types.js';
27
+ /**
28
+ * Main gRPC client for making type-safe API calls.
29
+ *
30
+ * This class manages the underlying transport and provides a factory method
31
+ * for creating type-safe request builders. Each request builder is bound to
32
+ * a specific service and method, ensuring compile-time type safety.
33
+ *
34
+ * The client handles:
35
+ * - Transport configuration (HTTP/1.1)
36
+ * - Request building with fluent API
37
+ * - Header management (auth should be added via .addHeader())
38
+ * - Error handling and logging
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Initialize the client
43
+ * const client = new GrpcClient('https://api.scrawn.dev');
44
+ *
45
+ * // Fetch credentials from your auth system
46
+ * const creds = await getCredentials();
47
+ *
48
+ * // Make a call to EventService.registerEvent
49
+ * const eventResponse = await client
50
+ * .newCall(EventService, 'registerEvent')
51
+ * .addHeader('Authorization', `Bearer ${creds.apiKey}`)
52
+ * .addPayload({ type: EventType.SDK_CALL, userId: 'u123', ... })
53
+ * .request();
54
+ *
55
+ * // Make a call to AuthService.createAPIKey
56
+ * const authResponse = await client
57
+ * .newCall(AuthService, 'createAPIKey')
58
+ * .addHeader('Authorization', `Bearer ${creds.apiKey}`)
59
+ * .addPayload({ name: 'My Key', expiresIn: 3600n })
60
+ * .request();
61
+ * ```
62
+ */
63
+ export declare class GrpcClient {
64
+ /** The underlying gRPC transport */
65
+ private readonly transport;
66
+ /** Base URL for API calls */
67
+ private readonly baseURL;
68
+ /**
69
+ * Create a new GrpcClient.
70
+ *
71
+ * @param baseURL - The base URL of the gRPC API (e.g., 'https://api.scrawn.dev')
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const client = new GrpcClient('https://api.scrawn.dev');
76
+ * ```
77
+ */
78
+ constructor(baseURL: string);
79
+ /**
80
+ * Create a new request builder for a specific service method.
81
+ *
82
+ * This is the entry point for making gRPC calls. The method is fully type-safe:
83
+ * - Service parameter must be a valid gRPC service
84
+ * - Method name must exist on the service (autocomplete provided)
85
+ * - Payload type is inferred from the method
86
+ * - Response type is inferred from the method
87
+ *
88
+ * @template S - The gRPC service type (auto-inferred)
89
+ * @template M - The method name (auto-inferred and validated)
90
+ *
91
+ * @param service - The gRPC service definition (e.g., EventService, AuthService)
92
+ * @param method - The method name as a string literal (e.g., 'registerEvent', 'createAPIKey')
93
+ * @returns A new RequestBuilder for chaining headers, payload, and execution
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * // EventService.registerEvent
98
+ * const eventBuilder = client.newCall(EventService, 'registerEvent');
99
+ * // Payload type is RegisterEventRequest
100
+ * // Response type is RegisterEventResponse
101
+ *
102
+ * // AuthService.createAPIKey
103
+ * const authBuilder = client.newCall(AuthService, 'createAPIKey');
104
+ * // Payload type is CreateAPIKeyRequest
105
+ * // Response type is CreateAPIKeyResponse
106
+ *
107
+ * // Type error - method doesn't exist!
108
+ * // const invalid = client.newCall(EventService, 'nonExistentMethod');
109
+ * ```
110
+ */
111
+ newCall<S extends ServiceType, M extends ServiceMethodNames<S>>(service: S, method: M): RequestBuilder<S, M>;
112
+ /**
113
+ * Get the base URL of this client.
114
+ *
115
+ * @returns The base URL string
116
+ */
117
+ getBaseURL(): string;
118
+ /**
119
+ * Get the underlying transport (for advanced use cases).
120
+ *
121
+ * @returns The Connect transport instance
122
+ * @internal
123
+ */
124
+ getTransport(): Transport;
125
+ }
126
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/core/grpc/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAKrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,UAAU;IACrB,oCAAoC;IACpC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IAEtC,6BAA6B;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC;;;;;;;;;OASG;gBACS,OAAO,EAAE,MAAM;IAa3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,SAAS,kBAAkB,CAAC,CAAC,CAAC,EAC5D,OAAO,EAAE,CAAC,EACV,MAAM,EAAE,CAAC,GACR,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;IAKvB;;;;OAIG;IACH,UAAU,IAAI,MAAM;IAIpB;;;;;OAKG;IACH,YAAY,IAAI,SAAS;CAG1B"}
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Type-safe gRPC client abstraction layer.
3
+ *
4
+ * This module provides the main entry point for making gRPC calls with
5
+ * full compile-time type safety and a beautiful fluent API.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const client = new GrpcClient('https://api.scrawn.dev');
10
+ *
11
+ * const response = await client
12
+ * .newCall(EventService, 'registerEvent')
13
+ * .addHeader('Authorization', `Bearer ${apiKey}`)
14
+ * .addHeader('x-request-id', '123')
15
+ * .addPayload({
16
+ * type: EventType.SDK_CALL,
17
+ * userId: 'user_123',
18
+ * data: { case: 'sdkCall', value: new SDKCall({ sdkCallType: SDKCallType.RAW, debitAmount: 10 }) }
19
+ * })
20
+ * .request();
21
+ * ```
22
+ */
23
+ import { createConnectTransport } from '@connectrpc/connect-node';
24
+ import { RequestBuilder } from './requestBuilder.js';
25
+ import { ScrawnLogger } from '../../utils/logger.js';
26
+ import { ScrawnConfig } from '../../config.js';
27
+ const log = new ScrawnLogger('GrpcClient');
28
+ /**
29
+ * Main gRPC client for making type-safe API calls.
30
+ *
31
+ * This class manages the underlying transport and provides a factory method
32
+ * for creating type-safe request builders. Each request builder is bound to
33
+ * a specific service and method, ensuring compile-time type safety.
34
+ *
35
+ * The client handles:
36
+ * - Transport configuration (HTTP/1.1)
37
+ * - Request building with fluent API
38
+ * - Header management (auth should be added via .addHeader())
39
+ * - Error handling and logging
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * // Initialize the client
44
+ * const client = new GrpcClient('https://api.scrawn.dev');
45
+ *
46
+ * // Fetch credentials from your auth system
47
+ * const creds = await getCredentials();
48
+ *
49
+ * // Make a call to EventService.registerEvent
50
+ * const eventResponse = await client
51
+ * .newCall(EventService, 'registerEvent')
52
+ * .addHeader('Authorization', `Bearer ${creds.apiKey}`)
53
+ * .addPayload({ type: EventType.SDK_CALL, userId: 'u123', ... })
54
+ * .request();
55
+ *
56
+ * // Make a call to AuthService.createAPIKey
57
+ * const authResponse = await client
58
+ * .newCall(AuthService, 'createAPIKey')
59
+ * .addHeader('Authorization', `Bearer ${creds.apiKey}`)
60
+ * .addPayload({ name: 'My Key', expiresIn: 3600n })
61
+ * .request();
62
+ * ```
63
+ */
64
+ export class GrpcClient {
65
+ /**
66
+ * Create a new GrpcClient.
67
+ *
68
+ * @param baseURL - The base URL of the gRPC API (e.g., 'https://api.scrawn.dev')
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const client = new GrpcClient('https://api.scrawn.dev');
73
+ * ```
74
+ */
75
+ constructor(baseURL) {
76
+ this.baseURL = baseURL;
77
+ log.info(`Initializing gRPC client for ${baseURL}`);
78
+ this.transport = createConnectTransport({
79
+ baseUrl: this.baseURL,
80
+ httpVersion: ScrawnConfig.grpc.httpVersion,
81
+ });
82
+ log.info('gRPC client initialized successfully');
83
+ }
84
+ /**
85
+ * Create a new request builder for a specific service method.
86
+ *
87
+ * This is the entry point for making gRPC calls. The method is fully type-safe:
88
+ * - Service parameter must be a valid gRPC service
89
+ * - Method name must exist on the service (autocomplete provided)
90
+ * - Payload type is inferred from the method
91
+ * - Response type is inferred from the method
92
+ *
93
+ * @template S - The gRPC service type (auto-inferred)
94
+ * @template M - The method name (auto-inferred and validated)
95
+ *
96
+ * @param service - The gRPC service definition (e.g., EventService, AuthService)
97
+ * @param method - The method name as a string literal (e.g., 'registerEvent', 'createAPIKey')
98
+ * @returns A new RequestBuilder for chaining headers, payload, and execution
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * // EventService.registerEvent
103
+ * const eventBuilder = client.newCall(EventService, 'registerEvent');
104
+ * // Payload type is RegisterEventRequest
105
+ * // Response type is RegisterEventResponse
106
+ *
107
+ * // AuthService.createAPIKey
108
+ * const authBuilder = client.newCall(AuthService, 'createAPIKey');
109
+ * // Payload type is CreateAPIKeyRequest
110
+ * // Response type is CreateAPIKeyResponse
111
+ *
112
+ * // Type error - method doesn't exist!
113
+ * // const invalid = client.newCall(EventService, 'nonExistentMethod');
114
+ * ```
115
+ */
116
+ newCall(service, method) {
117
+ log.debug(`Creating new request builder for ${service.typeName}.${method}`);
118
+ return new RequestBuilder(this.transport, service, method);
119
+ }
120
+ /**
121
+ * Get the base URL of this client.
122
+ *
123
+ * @returns The base URL string
124
+ */
125
+ getBaseURL() {
126
+ return this.baseURL;
127
+ }
128
+ /**
129
+ * Get the underlying transport (for advanced use cases).
130
+ *
131
+ * @returns The Connect transport instance
132
+ * @internal
133
+ */
134
+ getTransport() {
135
+ return this.transport;
136
+ }
137
+ }
138
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/core/grpc/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,GAAG,GAAG,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,OAAO,UAAU;IAOrB;;;;;;;;;OASG;IACH,YAAY,OAAe;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,GAAG,CAAC,IAAI,CAAC,gCAAgC,OAAO,EAAE,CAAC,CAAC;QAEpD,IAAI,CAAC,SAAS,GAAG,sBAAsB,CAAC;YACtC,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,YAAY,CAAC,IAAI,CAAC,WAAW;SAC3C,CAAC,CAAC;QAEH,GAAG,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAC;IACnD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,OAAO,CACL,OAAU,EACV,MAAS;QAET,GAAG,CAAC,KAAK,CAAC,oCAAoC,OAAO,CAAC,QAAQ,IAAI,MAAM,EAAE,CAAC,CAAC;QAC5E,OAAO,IAAI,cAAc,CAAO,IAAI,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACnE,CAAC;IAED;;;;OAIG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;;OAKG;IACH,YAAY;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;CACF"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * gRPC abstraction layer - Type-safe fluent API for gRPC calls.
3
+ *
4
+ * This module provides a beautiful, type-safe interface for making gRPC calls
5
+ * with automatic type inference, compile-time validation, and a fluent API.
6
+ *
7
+ * @module grpc
8
+ */
9
+ export { GrpcClient } from './client.js';
10
+ export { RequestBuilder } from './requestBuilder.js';
11
+ export type { ServiceMethodNames, MethodInput, MethodOutput, Headers, RequestState, } from './types.js';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/grpc/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,YAAY,EACV,kBAAkB,EAClB,WAAW,EACX,YAAY,EACZ,OAAO,EACP,YAAY,GACb,MAAM,YAAY,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * gRPC abstraction layer - Type-safe fluent API for gRPC calls.
3
+ *
4
+ * This module provides a beautiful, type-safe interface for making gRPC calls
5
+ * with automatic type inference, compile-time validation, and a fluent API.
6
+ *
7
+ * @module grpc
8
+ */
9
+ export { GrpcClient } from './client.js';
10
+ export { RequestBuilder } from './requestBuilder.js';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/grpc/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}