@timbrix/sdk 0.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.
@@ -0,0 +1,684 @@
1
+ import { KyInstance, Options } from 'ky';
2
+
3
+ type AuthMode = "bearer" | "apiKey";
4
+ interface TimbrixConfig {
5
+ baseUrl?: string;
6
+ apiKey?: string;
7
+ bearerToken?: string;
8
+ }
9
+ interface TimbrixUser {
10
+ id: string;
11
+ email: string;
12
+ createdAt?: string;
13
+ updatedAt?: string;
14
+ }
15
+ interface LoginInput {
16
+ email: string;
17
+ password: string;
18
+ }
19
+ interface LoginResponse {
20
+ access_token: string;
21
+ user?: TimbrixUser;
22
+ }
23
+ interface Organization {
24
+ id: string;
25
+ name: string;
26
+ slug: string;
27
+ logo?: string | null;
28
+ plan: "free" | "pro" | "enterprise";
29
+ ownerId: string;
30
+ createdAt: string;
31
+ updatedAt: string;
32
+ }
33
+ interface CreateOrganizationInput {
34
+ name: string;
35
+ slug: string;
36
+ logo?: string;
37
+ }
38
+ interface UpdateOrganizationInput {
39
+ name?: string;
40
+ logo?: string;
41
+ }
42
+ interface OrganizationDetails {
43
+ id: string;
44
+ slug: string;
45
+ logo_url?: string;
46
+ timezone: string;
47
+ created_at: string;
48
+ updated_at: string;
49
+ is_production_ready: boolean;
50
+ pending_steps?: Array<{
51
+ type: string;
52
+ description: string;
53
+ }>;
54
+ legal: {
55
+ name?: string;
56
+ legal_name?: string;
57
+ tax_system?: string;
58
+ website?: string;
59
+ phone?: string;
60
+ rfc?: string;
61
+ zip?: string;
62
+ address?: {
63
+ street?: string;
64
+ exterior?: string;
65
+ interior?: string;
66
+ neighborhood?: string;
67
+ city?: string;
68
+ municipality?: string;
69
+ zip?: string;
70
+ state?: string;
71
+ };
72
+ };
73
+ customization: {
74
+ has_logo: boolean;
75
+ color?: string;
76
+ next_folio_number?: number;
77
+ next_folio_number_test?: number;
78
+ pdf_extra?: {
79
+ codes?: boolean;
80
+ address_codes?: boolean;
81
+ product_key?: boolean;
82
+ round_unit_price?: boolean;
83
+ tax_breakdown?: boolean;
84
+ ieps_breakdown?: boolean;
85
+ render_carta_porte?: boolean;
86
+ repeat_signature?: boolean;
87
+ };
88
+ };
89
+ certificate: {
90
+ has_certificate: boolean;
91
+ updated_at?: string;
92
+ expires_at?: string;
93
+ serial_number?: string;
94
+ };
95
+ fiel: {
96
+ has_fiel: boolean;
97
+ updated_at?: string;
98
+ expires_at?: string;
99
+ serial_number?: string;
100
+ };
101
+ receipts: {
102
+ periodicity?: string;
103
+ duration_days?: number;
104
+ next_folio_number?: number;
105
+ next_folio_number_test?: number;
106
+ };
107
+ self_invoice: {
108
+ allowed_cfdi_uses?: string[];
109
+ apply_resico_isr?: boolean;
110
+ support_email?: string;
111
+ support_email_verified?: boolean;
112
+ };
113
+ }
114
+ type OrganizationRole = "owner" | "admin" | "member";
115
+ interface OrganizationMember {
116
+ id: string;
117
+ userId: string;
118
+ organizationId: string;
119
+ role: OrganizationRole;
120
+ user?: {
121
+ id: string;
122
+ email: string;
123
+ };
124
+ joinedAt?: string;
125
+ createdAt?: string;
126
+ }
127
+ interface OrganizationInvite {
128
+ id: string;
129
+ organizationId: string;
130
+ email: string;
131
+ role: Exclude<OrganizationRole, "owner">;
132
+ invitedById: string;
133
+ createdAt: string;
134
+ expiresAt?: string;
135
+ }
136
+ interface InviteMemberInput {
137
+ email: string;
138
+ role: "admin" | "member";
139
+ }
140
+ interface UpdateMemberRoleInput {
141
+ role: "admin" | "member";
142
+ }
143
+ interface LegalAddress {
144
+ street: string;
145
+ exterior: number;
146
+ interior?: number;
147
+ neighborhood: string;
148
+ city: string;
149
+ municipality: string;
150
+ zip: number;
151
+ state: string;
152
+ }
153
+ interface UpdateLegalDataInput {
154
+ name?: string;
155
+ legal_name?: string;
156
+ tax_system?: string;
157
+ website?: string;
158
+ support_email?: string;
159
+ phone?: string;
160
+ address?: LegalAddress;
161
+ }
162
+ interface ValidateCertificateResponse {
163
+ valid: boolean;
164
+ rfc?: string;
165
+ serialNumber?: string;
166
+ expiresAt?: string;
167
+ issuedAt?: string;
168
+ subjectName?: string;
169
+ isExpired?: boolean;
170
+ passwordValid?: boolean;
171
+ passwordError?: string;
172
+ error?: string;
173
+ }
174
+ type WebhookEvent = "organization.created" | "organization.updated" | "organization.deleted" | "member.added" | "member.removed" | "member.role_updated" | "invite.created" | "invite.accepted" | "invite.cancelled" | "oauth_app.created" | "oauth_app.revoked" | "webhook.created";
175
+ interface Webhook {
176
+ id: string;
177
+ organizationId: string;
178
+ url: string;
179
+ events: WebhookEvent[];
180
+ isActive: boolean;
181
+ secret?: string;
182
+ createdAt: string;
183
+ updatedAt: string;
184
+ }
185
+ interface CreateWebhookInput {
186
+ url: string;
187
+ events: WebhookEvent[];
188
+ }
189
+ interface UpdateWebhookInput {
190
+ url?: string;
191
+ events?: WebhookEvent[];
192
+ isActive?: boolean;
193
+ }
194
+ interface WebhookDelivery {
195
+ id: string;
196
+ webhookId: string;
197
+ event: WebhookEvent;
198
+ statusCode: number;
199
+ responseBody: string;
200
+ attempts: number;
201
+ success: boolean;
202
+ createdAt: string;
203
+ }
204
+ type ApiKeyScope = "read:user" | "write:user" | "read:organization" | "write:organization" | "read:members" | "write:members" | "read:webhooks" | "write:webhooks" | "read:api-keys" | "write:api-keys";
205
+ interface ApiKey {
206
+ id: string;
207
+ organizationId: string;
208
+ name: string;
209
+ description?: string | null;
210
+ keyPreview: string;
211
+ scopes: ApiKeyScope[];
212
+ isActive: boolean;
213
+ rateLimitPerMinute: number;
214
+ rateLimitPerHour: number;
215
+ allowedIps?: string[] | null;
216
+ expiresAt?: string | null;
217
+ lastUsedAt?: string | null;
218
+ usageCount: number;
219
+ createdBy: string;
220
+ createdAt: string;
221
+ updatedAt: string;
222
+ }
223
+ interface CreateApiKeyInput {
224
+ name: string;
225
+ description?: string;
226
+ scopes?: ApiKeyScope[];
227
+ rateLimitPerMinute?: number;
228
+ rateLimitPerHour?: number;
229
+ allowedIps?: string[];
230
+ expiresAt?: string;
231
+ }
232
+ interface CreateApiKeyResponse {
233
+ plainKey: string;
234
+ apiKey: Omit<ApiKey, "usageCount" | "lastUsedAt" | "createdBy" | "updatedAt">;
235
+ }
236
+ type UpdateApiKeyInput = Partial<CreateApiKeyInput>;
237
+ interface ApiKeyStats {
238
+ totalRequests: number;
239
+ last24Hours: number;
240
+ last7Days: number;
241
+ last30Days: number;
242
+ }
243
+ interface ApiKeyValidateResponse {
244
+ valid: boolean;
245
+ keyId: string;
246
+ keyName: string;
247
+ organizationId: string;
248
+ scopes: ApiKeyScope[];
249
+ expiresAt?: string | null;
250
+ isActive: boolean;
251
+ }
252
+ interface CustomerAddress {
253
+ street: string;
254
+ exterior: string;
255
+ interior?: string | null;
256
+ neighborhood: string;
257
+ city: string;
258
+ municipality: string;
259
+ zip: string;
260
+ state: string;
261
+ country: string;
262
+ }
263
+ interface Customer {
264
+ id: string;
265
+ organizationId: string;
266
+ legalName: string;
267
+ taxId: string;
268
+ taxSystem: string;
269
+ email: string;
270
+ phone: string | null;
271
+ defaultInvoiceUse: string;
272
+ address: CustomerAddress;
273
+ createdAt: string;
274
+ updatedAt: string;
275
+ }
276
+ interface CreateCustomerInput {
277
+ legalName: string;
278
+ taxId: string;
279
+ taxSystem: string;
280
+ email: string;
281
+ phone?: string;
282
+ defaultInvoiceUse: string;
283
+ addressStreet: string;
284
+ addressExterior: string;
285
+ addressInterior?: string;
286
+ addressNeighborhood: string;
287
+ addressCity: string;
288
+ addressMunicipality: string;
289
+ addressZip: string;
290
+ addressState: string;
291
+ addressCountry?: string;
292
+ }
293
+ type UpdateCustomerInput = Partial<CreateCustomerInput>;
294
+ type OAuthScope = "read:user" | "write:user" | "read:organization" | "write:organization" | "read:members" | "write:members" | "read:webhooks" | "write:webhooks" | "admin:organization" | "admin:all";
295
+ interface OAuthApp {
296
+ clientId: string;
297
+ name: string;
298
+ description?: string | null;
299
+ redirectUri?: string | null;
300
+ scopes: OAuthScope[];
301
+ isActive: boolean;
302
+ createdBy: string;
303
+ createdAt: string;
304
+ updatedAt: string;
305
+ }
306
+ interface OAuthAppWithSecret extends OAuthApp {
307
+ clientSecret: string;
308
+ }
309
+ interface CreateOAuthAppInput {
310
+ name: string;
311
+ scopes: OAuthScope[];
312
+ description?: string;
313
+ redirectUri?: string;
314
+ }
315
+ type UpdateOAuthAppInput = Partial<Omit<CreateOAuthAppInput, "scopes"> & {
316
+ scopes?: OAuthScope[];
317
+ isActive?: boolean;
318
+ }>;
319
+ interface OAuthToken {
320
+ id: string;
321
+ clientId: string;
322
+ userId?: string | null;
323
+ scopes: OAuthScope[];
324
+ expiresAt: string;
325
+ createdAt: string;
326
+ lastUsedAt?: string | null;
327
+ }
328
+ interface TokenResponse {
329
+ access_token: string;
330
+ token_type: "Bearer";
331
+ expires_in: number;
332
+ scope: string;
333
+ refresh_token?: string;
334
+ }
335
+ interface GenerateTokenInput {
336
+ clientId: string;
337
+ clientSecret: string;
338
+ scopes: OAuthScope[];
339
+ userId?: string;
340
+ }
341
+ interface ExchangeCodeInput {
342
+ code: string;
343
+ clientId: string;
344
+ clientSecret: string;
345
+ redirectUri: string;
346
+ codeVerifier?: string;
347
+ }
348
+ interface RefreshTokenInput {
349
+ refreshToken: string;
350
+ }
351
+ interface ProductTax {
352
+ type: string;
353
+ rate: number;
354
+ withholding?: boolean;
355
+ factor?: string;
356
+ }
357
+ interface Product {
358
+ id: string;
359
+ organizationId: string;
360
+ livemode: boolean;
361
+ description: string;
362
+ productKey: number;
363
+ price: number;
364
+ taxIncluded: boolean;
365
+ taxability: string;
366
+ taxes: ProductTax[];
367
+ localTaxes: ProductTax[];
368
+ unitKey: string;
369
+ unitName: string;
370
+ sku: string | null;
371
+ createdAt: string;
372
+ updatedAt: string;
373
+ }
374
+ interface CreateProductInput {
375
+ description: string;
376
+ productKey: number;
377
+ price: number;
378
+ taxIncluded?: boolean;
379
+ taxability?: string;
380
+ taxes?: ProductTax[];
381
+ localTaxes?: ProductTax[];
382
+ unitKey?: string;
383
+ unitName?: string;
384
+ sku?: string;
385
+ livemode?: boolean;
386
+ }
387
+ type UpdateProductInput = Partial<CreateProductInput>;
388
+ interface ListProductsFilters {
389
+ description?: string;
390
+ sku?: string;
391
+ productKey?: number;
392
+ }
393
+ interface TimbrixError {
394
+ statusCode: number;
395
+ message: string | string[];
396
+ error?: string;
397
+ }
398
+
399
+ declare class ApiKeysResource {
400
+ private http;
401
+ constructor(http: KyInstance);
402
+ /**
403
+ * List all API keys for an organization
404
+ */
405
+ list(organizationId: string): Promise<ApiKey[]>;
406
+ /**
407
+ * Get API key by ID
408
+ */
409
+ get(organizationId: string, apiKeyId: string): Promise<ApiKey>;
410
+ /**
411
+ * Create a new API key. The plain key is returned only once in the response.
412
+ */
413
+ create(organizationId: string, data: CreateApiKeyInput): Promise<CreateApiKeyResponse>;
414
+ /**
415
+ * Update API key
416
+ */
417
+ update(organizationId: string, apiKeyId: string, data: UpdateApiKeyInput): Promise<ApiKey>;
418
+ /**
419
+ * Delete (revoke) API key
420
+ */
421
+ delete(organizationId: string, apiKeyId: string): Promise<void>;
422
+ /**
423
+ * Get API key usage statistics for an organization
424
+ */
425
+ stats(organizationId: string): Promise<ApiKeyStats>;
426
+ /**
427
+ * Validate the currently authenticated API key (uses X-API-Key header)
428
+ */
429
+ validate(): Promise<ApiKeyValidateResponse>;
430
+ }
431
+
432
+ declare class AuthResource {
433
+ private http;
434
+ constructor(http: KyInstance);
435
+ /**
436
+ * Login with email and password. Returns an access token.
437
+ */
438
+ login(data: LoginInput): Promise<LoginResponse>;
439
+ /**
440
+ * Logout and revoke the current access token.
441
+ */
442
+ logout(): Promise<void>;
443
+ }
444
+
445
+ declare class CustomersResource {
446
+ private http;
447
+ constructor(http: KyInstance);
448
+ /**
449
+ * List all customers for an organization
450
+ */
451
+ list(organizationId: string): Promise<Customer[]>;
452
+ /**
453
+ * Get a customer by ID
454
+ */
455
+ get(organizationId: string, customerId: string): Promise<Customer>;
456
+ /**
457
+ * Create a new customer
458
+ */
459
+ create(organizationId: string, data: CreateCustomerInput): Promise<Customer>;
460
+ /**
461
+ * Update a customer
462
+ */
463
+ update(organizationId: string, customerId: string, data: UpdateCustomerInput): Promise<Customer>;
464
+ /**
465
+ * Delete a customer
466
+ */
467
+ delete(organizationId: string, customerId: string): Promise<void>;
468
+ }
469
+
470
+ declare class OAuthResource {
471
+ private http;
472
+ constructor(http: KyInstance);
473
+ /**
474
+ * Create a new OAuth application. Client secret is returned only once.
475
+ */
476
+ createApp(data: CreateOAuthAppInput): Promise<OAuthAppWithSecret>;
477
+ /**
478
+ * Get OAuth application by client ID
479
+ */
480
+ getApp(clientId: string): Promise<OAuthApp>;
481
+ /**
482
+ * List OAuth applications for an organization
483
+ */
484
+ listApps(organizationId: string): Promise<OAuthApp[]>;
485
+ /**
486
+ * Update OAuth application
487
+ */
488
+ updateApp(clientId: string, data: UpdateOAuthAppInput): Promise<OAuthApp>;
489
+ /**
490
+ * Delete OAuth application. All associated tokens will be revoked.
491
+ */
492
+ deleteApp(clientId: string): Promise<void>;
493
+ /**
494
+ * List active tokens for an OAuth application
495
+ */
496
+ listTokens(clientId: string): Promise<OAuthToken[]>;
497
+ /**
498
+ * Revoke an OAuth token (requires write:oauth-apps scope)
499
+ */
500
+ revokeToken(tokenId: string): Promise<void>;
501
+ /**
502
+ * Generate an access token using client credentials flow
503
+ */
504
+ generateToken(data: GenerateTokenInput): Promise<TokenResponse>;
505
+ /**
506
+ * Exchange an authorization code for access and refresh tokens
507
+ */
508
+ exchangeCode(data: ExchangeCodeInput): Promise<TokenResponse>;
509
+ /**
510
+ * Refresh an access token using a refresh token
511
+ */
512
+ refreshToken(data: RefreshTokenInput): Promise<TokenResponse>;
513
+ }
514
+
515
+ declare class OrganizationsResource {
516
+ private http;
517
+ constructor(http: KyInstance);
518
+ /**
519
+ * Create a new organization
520
+ */
521
+ create(data: CreateOrganizationInput): Promise<Organization>;
522
+ /**
523
+ * Get organization by ID
524
+ */
525
+ get(id: string): Promise<OrganizationDetails>;
526
+ /**
527
+ * Update organization (owner only)
528
+ */
529
+ update(id: string, data: UpdateOrganizationInput): Promise<Organization>;
530
+ /**
531
+ * Update organization legal/fiscal data (owner only)
532
+ */
533
+ updateLegalData(id: string, data: UpdateLegalDataInput): Promise<OrganizationDetails>;
534
+ /**
535
+ * Delete organization (owner only)
536
+ */
537
+ delete(id: string): Promise<void>;
538
+ /**
539
+ * Get organization members
540
+ */
541
+ members(id: string): Promise<OrganizationMember[]>;
542
+ /**
543
+ * Invite a member (owner/admin only)
544
+ */
545
+ invite(id: string, data: InviteMemberInput): Promise<OrganizationInvite>;
546
+ /**
547
+ * Remove a member (owner/admin only)
548
+ */
549
+ removeMember(id: string, memberId: string): Promise<void>;
550
+ /**
551
+ * Update member role (owner/admin only)
552
+ */
553
+ updateMemberRole(id: string, memberId: string, data: UpdateMemberRoleInput): Promise<OrganizationMember>;
554
+ /**
555
+ * Get pending invitations
556
+ */
557
+ invites(id: string): Promise<OrganizationInvite[]>;
558
+ /**
559
+ * Cancel a pending invitation (owner/admin only)
560
+ */
561
+ cancelInvite(id: string, inviteId: string): Promise<void>;
562
+ }
563
+
564
+ declare class ProductsResource {
565
+ private http;
566
+ constructor(http: KyInstance);
567
+ /**
568
+ * List products for an organization, with optional filters
569
+ */
570
+ list(organizationId: string, filters?: ListProductsFilters): Promise<Product[]>;
571
+ /**
572
+ * Get a product by ID
573
+ */
574
+ get(organizationId: string, productId: string): Promise<Product>;
575
+ /**
576
+ * Create a new product or service
577
+ */
578
+ create(organizationId: string, data: CreateProductInput): Promise<Product>;
579
+ /**
580
+ * Update a product
581
+ */
582
+ update(organizationId: string, productId: string, data: UpdateProductInput): Promise<Product>;
583
+ /**
584
+ * Delete a product
585
+ */
586
+ delete(organizationId: string, productId: string): Promise<void>;
587
+ }
588
+
589
+ declare class UsersResource {
590
+ private http;
591
+ constructor(http: KyInstance);
592
+ /**
593
+ * Get the currently authenticated user
594
+ */
595
+ me(): Promise<TimbrixUser>;
596
+ /**
597
+ * Get user by ID (requires read:user scope)
598
+ */
599
+ getById(id: string): Promise<TimbrixUser>;
600
+ /**
601
+ * Get organizations for a user (requires read:organization scope)
602
+ */
603
+ getOrganizations(id: string): Promise<OrganizationDetails[]>;
604
+ /**
605
+ * Get user by email (requires read:user scope)
606
+ */
607
+ getByEmail(email: string): Promise<TimbrixUser>;
608
+ }
609
+
610
+ declare class WebhooksResource {
611
+ private http;
612
+ constructor(http: KyInstance);
613
+ /**
614
+ * List all webhooks for an organization
615
+ */
616
+ list(organizationId: string): Promise<Webhook[]>;
617
+ /**
618
+ * Get webhook by ID
619
+ */
620
+ get(organizationId: string, webhookId: string): Promise<Webhook>;
621
+ /**
622
+ * Create a new webhook
623
+ */
624
+ create(organizationId: string, data: CreateWebhookInput): Promise<Webhook>;
625
+ /**
626
+ * Update webhook
627
+ */
628
+ update(organizationId: string, webhookId: string, data: UpdateWebhookInput): Promise<Webhook>;
629
+ /**
630
+ * Delete webhook
631
+ */
632
+ delete(organizationId: string, webhookId: string): Promise<void>;
633
+ /**
634
+ * Get webhook delivery history
635
+ */
636
+ deliveries(organizationId: string, webhookId: string): Promise<WebhookDelivery[]>;
637
+ /**
638
+ * Send test webhook
639
+ */
640
+ test(organizationId: string, webhookId: string): Promise<{
641
+ message: string;
642
+ }>;
643
+ }
644
+
645
+ declare class Timbrix {
646
+ private http;
647
+ private authStrategy?;
648
+ readonly auth: AuthResource;
649
+ readonly organizations: OrganizationsResource;
650
+ readonly webhooks: WebhooksResource;
651
+ readonly apiKeys: ApiKeysResource;
652
+ readonly users: UsersResource;
653
+ readonly customers: CustomersResource;
654
+ readonly oauth: OAuthResource;
655
+ readonly products: ProductsResource;
656
+ constructor(config?: TimbrixConfig);
657
+ /**
658
+ * Update authentication strategy (useful for CLI when token changes)
659
+ */
660
+ setAuth(bearerToken?: string, apiKey?: string): void;
661
+ /**
662
+ * Make a custom request
663
+ */
664
+ request<T>(url: string, options?: Options): Promise<T>;
665
+ }
666
+
667
+ interface AuthStrategy {
668
+ getHeaders(): Record<string, string>;
669
+ applyAuth(options: Options): Options;
670
+ }
671
+ declare class BearerAuth implements AuthStrategy {
672
+ private token;
673
+ constructor(token: string);
674
+ getHeaders(): Record<string, string>;
675
+ applyAuth(options: Options): Options;
676
+ }
677
+ declare class ApiKeyAuth implements AuthStrategy {
678
+ private apiKey;
679
+ constructor(apiKey: string);
680
+ getHeaders(): Record<string, string>;
681
+ applyAuth(options: Options): Options;
682
+ }
683
+
684
+ export { type ApiKey, ApiKeyAuth, type ApiKeyScope, type ApiKeyStats, type ApiKeyValidateResponse, ApiKeysResource, type AuthMode, AuthResource, type AuthStrategy, BearerAuth, type CreateApiKeyInput, type CreateApiKeyResponse, type CreateCustomerInput, type CreateOAuthAppInput, type CreateOrganizationInput, type CreateProductInput, type CreateWebhookInput, type Customer, type CustomerAddress, CustomersResource, type ExchangeCodeInput, type GenerateTokenInput, type InviteMemberInput, type LegalAddress, type ListProductsFilters, type LoginInput, type LoginResponse, type OAuthApp, type OAuthAppWithSecret, OAuthResource, type OAuthScope, type OAuthToken, type Organization, type OrganizationDetails, type OrganizationInvite, type OrganizationMember, type OrganizationRole, OrganizationsResource, type Product, type ProductTax, ProductsResource, type RefreshTokenInput, Timbrix, type TimbrixConfig, type TimbrixError, type TimbrixUser, type TokenResponse, type UpdateApiKeyInput, type UpdateCustomerInput, type UpdateLegalDataInput, type UpdateMemberRoleInput, type UpdateOAuthAppInput, type UpdateOrganizationInput, type UpdateProductInput, type UpdateWebhookInput, UsersResource, type ValidateCertificateResponse, type Webhook, type WebhookDelivery, type WebhookEvent, WebhooksResource };