Skip to content

Commit

Permalink
fix(backend): Align Session resource with OpenAPI spec (#4869)
Browse files Browse the repository at this point in the history
Co-authored-by: panteliselef <panteliselef@outlook.com>
  • Loading branch information
octoper and panteliselef authored Jan 13, 2025
1 parent 9a0fe7c commit 72d2953
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .changeset/bright-mangos-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@clerk/backend': minor
---

Update Session resource with new properties to align with OpenAPI spec
- `lastActiveOrganizationId`
- `latestActivity`
- `actor`
14 changes: 13 additions & 1 deletion packages/backend/src/api/resources/JSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,25 @@ export interface RedirectUrlJSON extends ClerkResourceJSON {
updated_at: number;
}

export interface SessionActivityJSON extends ClerkResourceJSON {
id: string;
device_type?: string;
is_mobile: boolean;
browser_name?: string;
browser_version?: string;
ip_address?: string;
city?: string;
country?: string;
}

export interface SessionJSON extends ClerkResourceJSON {
object: typeof ObjectType.Session;
client_id: string;
user_id: string;
status: string;
last_active_organization_id?: string;
actor?: Record<string, unknown>;
actor: Record<string, unknown> | null;
latest_activity?: SessionActivityJSON;
last_active_at: number;
expire_at: number;
abandon_at: number;
Expand Down
34 changes: 33 additions & 1 deletion packages/backend/src/api/resources/Session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
import type { SessionJSON } from './JSON';
import type { SessionActivityJSON, SessionJSON } from './JSON';

export class SessionActivity {
constructor(
readonly id: string,
readonly isMobile: boolean,
readonly ipAddress?: string,
readonly city?: string,
readonly country?: string,
readonly browserVersion?: string,
readonly browserName?: string,
readonly deviceType?: string,
) {}

static fromJSON(data: SessionActivityJSON): SessionActivity {
return new SessionActivity(
data.id,
data.is_mobile,
data.ip_address,
data.city,
data.country,
data.browser_version,
data.browser_name,
data.device_type,
);
}
}

export class Session {
constructor(
Expand All @@ -11,6 +37,9 @@ export class Session {
readonly abandonAt: number,
readonly createdAt: number,
readonly updatedAt: number,
readonly lastActiveOrganizationId?: string,
readonly latestActivity?: SessionActivity,
readonly actor: Record<string, unknown> | null = null,
) {}

static fromJSON(data: SessionJSON): Session {
Expand All @@ -24,6 +53,9 @@ export class Session {
data.abandon_at,
data.created_at,
data.updated_at,
data.last_active_organization_id,
data.latest_activity && SessionActivity.fromJSON(data.latest_activity),
data.actor,
);
}
}

0 comments on commit 72d2953

Please sign in to comment.