Skip to content

Commit

Permalink
Changeset types and set csrfToken for component manager
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Feb 8, 2022
1 parent cfb84e5 commit 7fc268c
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/server/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { SessionData } from "express-session";
import { WebSocket } from "ws";
import { LiveViewComponentManager } from "./socket/component_manager";
import { PhxOutgoingLivePatchPush } from "./socket/types";
import { HtmlSafeString } from "./templates";

// Validation errors for a type T should
// be keyed by the field name
export type LiveViewChangesetErrors<T> = {
[Property in keyof T]?: string
}

// Changeset represents the state of a form
// as it is validated and submitted by the user
export interface LiveViewChangeset<T> {
action?: string //
changes: Partial<T> // diff between initial and updated
errors?: LiveViewChangesetErrors<T> // validation errors by field name of T
data: T | Partial<T> // merged data
valid: boolean // true if no validation errors
}

export interface LiveViewSocket<T> {
id: string;
connected: boolean; // true for websocket, false for http request
Expand All @@ -21,9 +36,7 @@ export interface LiveViewMountParams {
["_mounts"]: number
}

export interface LiveViewSessionParams {
[key: string]: string;
}
export interface LiveViewSessionParams extends Record<string, string> { }

// params on url are strings
export type StringPropertyValues<Type> = { [Property in keyof Type]: string; };
Expand Down Expand Up @@ -69,6 +82,12 @@ export abstract class BaseLiveViewComponent<T, P> implements LiveViewComponent<T
}
}

csrfToken(): string | undefined {
if (this.componentManager) {
return this.componentManager.csrfToken;
}
}

registerComponentManager(manager: LiveViewComponentManager) {
this.componentManager = manager;
}
Expand Down

0 comments on commit 7fc268c

Please sign in to comment.