Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-rats-throw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws/lambda-invoke-store": minor
---

Add support for tenantId
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@ dist
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
!.yarn/versions

# Yarn is preferred in this package
package-lock.json
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ Convenience method to get the current request ID.
const requestId = InvokeStore.getRequestId(); // Returns '-' if outside context
```

### InvokeStore.getTenantId()

Convenience method to get the tenant ID.

```typescript
const requestId = InvokeStore.getTenantId();
```

### InvokeStore.getXRayTraceId()

Convenience method to get the current [X-Ray trace ID](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces). This ID is used for distributed tracing across AWS services.
Expand Down
9 changes: 8 additions & 1 deletion src/invoke-store.global.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ describe("InvokeStore Global Singleton", () => {
it("should maintain singleton behavior with dynamic imports", async () => {
// GIVEN
const testRequestId = "dynamic-import-test";
const testTenantId = "dynamic-import-tenant-id-test";
const testKey = "dynamic-key";
const testValue = "dynamic-value";

// WHEN - Set up context with original import
await OriginalImport.run(
{ [OriginalImport.PROTECTED_KEYS.REQUEST_ID]: testRequestId },
{
[OriginalImport.PROTECTED_KEYS.REQUEST_ID]: testRequestId,
[OriginalImport.PROTECTED_KEYS.TENANT_ID]: testTenantId,
},
async () => {
OriginalImport.set(testKey, testValue);

Expand All @@ -91,6 +95,7 @@ describe("InvokeStore Global Singleton", () => {
// THEN - Dynamically imported instance should see the same context
expect(DynamicImport).toBe(OriginalImport); // Same instance
expect(DynamicImport.getRequestId()).toBe(testRequestId);
expect(DynamicImport.getTenantId()).toBe(testTenantId);
expect(DynamicImport.get(testKey)).toBe(testValue);

// WHEN - Set a new value using dynamic import
Expand Down Expand Up @@ -132,6 +137,7 @@ describe("InvokeStore Existing Instance", () => {
set: vi.fn(),
getRequestId: vi.fn().mockReturnValue("mock-request-id"),
getXRayTraceId: vi.fn(),
getTenantId: vi.fn().mockReturnValue("my-test-tenant-id"),
hasContext: vi.fn(),
};

Expand All @@ -144,6 +150,7 @@ describe("InvokeStore Existing Instance", () => {
// THEN
expect(ReimportedStore).toBe(mockInstance);
expect(ReimportedStore.getRequestId()).toBe("mock-request-id");
expect(ReimportedStore.getTenantId()).toBe("my-test-tenant-id");
});
});

Expand Down
8 changes: 8 additions & 0 deletions src/invoke-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (!noGlobalAwsLambda) {
const PROTECTED_KEYS = {
REQUEST_ID: Symbol("_AWS_LAMBDA_REQUEST_ID"),
X_RAY_TRACE_ID: Symbol("_AWS_LAMBDA_X_RAY_TRACE_ID"),
TENANT_ID: Symbol("_AWS_LAMBDA_TENANT_ID"),
} as const;

/**
Expand Down Expand Up @@ -85,6 +86,13 @@ class InvokeStoreImpl {
return this.get<string>(this.PROTECTED_KEYS.X_RAY_TRACE_ID);
}

/**
* Get the current tenant ID
*/
public static getTenantId(): string | undefined {
return this.get<string>(this.PROTECTED_KEYS.TENANT_ID);
}

/**
* Check if we're currently within an invoke context
*/
Expand Down