Skip to content

Commit

Permalink
fix: fix meta type to allow nested json (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluvrt authored Oct 22, 2024
1 parent df66865 commit 98d7585
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-ghosts-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pluv/platform-cloudflare": patch
"@pluv/platform-node": patch
---

Update `meta` type on platform contexts to allow for nested json types.
11 changes: 4 additions & 7 deletions packages/platform-cloudflare/src/CloudflarePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ import type {
} from "@pluv/io";
import { AbstractPlatform } from "@pluv/io";
import { PersistenceCloudflareTransactionalStorage } from "@pluv/persistence-cloudflare-transactional-storage";
import type { JsonPrimitive } from "@pluv/types";
import type { Json } from "@pluv/types";
import { CloudflareWebSocket } from "./CloudflareWebSocket";
import { DEFAULT_REGISTRATION_MODE } from "./constants";

export type CloudflarePlatformRoomContext<
TEnv extends Record<string, any>,
TMeta extends Record<string, JsonPrimitive>,
> = {
export type CloudflarePlatformRoomContext<TEnv extends Record<string, any>, TMeta extends Record<string, Json>> = {
env: TEnv;
state: DurableObjectState;
} & (keyof TMeta extends never ? { meta?: undefined } : { meta: TMeta });

export type CloudflarePlatformConfig<
TEnv extends Record<string, any> = {},
TMeta extends Record<string, JsonPrimitive> = {},
TMeta extends Record<string, Json> = {},
> = AbstractPlatformConfig<CloudflarePlatformRoomContext<TEnv, TMeta>> & { mode?: WebSocketRegistrationMode };

export class CloudflarePlatform<
TEnv extends Record<string, any> = {},
TMeta extends Record<string, JsonPrimitive> = {},
TMeta extends Record<string, Json> = {},
> extends AbstractPlatform<
CloudflareWebSocket,
{ env: TEnv; request: Request },
Expand Down
7 changes: 2 additions & 5 deletions packages/platform-cloudflare/src/platformCloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { JsonPrimitive } from "@pluv/types";
import type { Json } from "@pluv/types";
import type { CloudflarePlatformConfig } from "./CloudflarePlatform";
import { CloudflarePlatform } from "./CloudflarePlatform";

export const platformCloudflare = <
TEnv extends Record<string, any> = {},
TMeta extends Record<string, JsonPrimitive> = {},
>(
export const platformCloudflare = <TEnv extends Record<string, any> = {}, TMeta extends Record<string, Json> = {}>(
config: CloudflarePlatformConfig<TEnv, TMeta> = {},
): CloudflarePlatform<TEnv, TMeta> => {
return new CloudflarePlatform<TEnv, TMeta>(config);
Expand Down
8 changes: 4 additions & 4 deletions packages/platform-node/src/NodePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import type {
WebSocketSerializedState,
} from "@pluv/io";
import { AbstractPlatform } from "@pluv/io";
import type { JsonPrimitive } from "@pluv/types";
import type { Json } from "@pluv/types";
import crypto from "node:crypto";
import type { IncomingMessage } from "node:http";
import { TextDecoder } from "node:util";
import type { WebSocket } from "ws";
import { NodeWebSocket } from "./NodeWebSocket";

export type NodePlatformRoomContext<TMeta extends Record<string, JsonPrimitive>> = keyof TMeta extends never
export type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never
? { meta?: undefined }
: { meta: TMeta };

export type NodePlatformConfig<TMeta extends Record<string, JsonPrimitive>> = { mode?: WebSocketRegistrationMode } & (
export type NodePlatformConfig<TMeta extends Record<string, Json>> = { mode?: WebSocketRegistrationMode } & (
| { persistence?: undefined; pubSub?: undefined }
| { persistence: AbstractPersistence; pubSub: AbstractPubSub }
) & { context?: NodePlatformRoomContext<TMeta> };

export class NodePlatform<TMeta extends Record<string, JsonPrimitive> = {}> extends AbstractPlatform<
export class NodePlatform<TMeta extends Record<string, Json> = {}> extends AbstractPlatform<
NodeWebSocket,
{ req: IncomingMessage },
NodePlatformRoomContext<TMeta>
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-node/src/platformNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { JsonPrimitive } from "@pluv/types";
import type { Json } from "@pluv/types";
import type { NodePlatformConfig } from "./NodePlatform";
import { NodePlatform } from "./NodePlatform";

export const platformNode = <TMeta extends Record<string, JsonPrimitive> = {}>(
export const platformNode = <TMeta extends Record<string, Json> = {}>(
config: NodePlatformConfig<TMeta> = {},
): NodePlatform<TMeta> => {
return new NodePlatform<TMeta>(config);
Expand Down

0 comments on commit 98d7585

Please sign in to comment.