Skip to content

Commit

Permalink
Refactor TS SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Oct 7, 2024
1 parent 30621f5 commit 30c3754
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 93 deletions.
2 changes: 1 addition & 1 deletion waspc/packages/wasp-config/src/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function mapAuth(
} = auth
return {
userEntity: parseEntityRef(userEntity),
// TODO: Abstract pattern
// TODO: Abstract away this pattern
...(externalAuthEntity && {
externalAuthEntity: parseEntityRef(externalAuthEntity),
}),
Expand Down
1 change: 1 addition & 0 deletions waspc/packages/wasp-config/src/private.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GET_USER_SPEC = Symbol('GET_USER_SPEC')
3 changes: 2 additions & 1 deletion waspc/packages/wasp-config/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { writeFileSync } from 'fs'
import { App } from './userApi.js'
import { Decl } from './appSpec.js'
import { mapUserSpecToDecls } from './mappers.js'
import { GET_USER_SPEC } from './private.js'

async function main() {
const {
Expand All @@ -19,7 +20,7 @@ async function main() {
}

function analyzeApp(app: App, entityNames: string[]): Decl[] {
const userSpec = app.getSpec()
const userSpec = app[GET_USER_SPEC]()
return mapUserSpecToDecls(userSpec, entityNames)
}

Expand Down
180 changes: 89 additions & 91 deletions waspc/packages/wasp-config/src/userApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,91 @@
import * as AppSpec from './appSpec.js'
import { GET_USER_SPEC } from './private.js'

export class App {
#userSpec: UserSpec;

[GET_USER_SPEC]() {
return this.#userSpec
}

constructor(name: string, config: AppConfig) {
this.#userSpec = {
app: { name, config: config },
actions: new Map(),
apiNamespaces: new Map(),
apis: new Map(),
auth: undefined,
client: undefined,
cruds: new Map(),
db: undefined,
emailSender: undefined,
jobs: new Map(),
pages: new Map(),
queries: new Map(),
routes: new Map(),
server: undefined,
websocket: undefined,
}
}

// TODO: Enforce that all methods are covered in compile time
action(this: App, name: string, config: ActionConfig): void {
this.#userSpec.actions.set(name, config)
}

apiNamespace(this: App, name: string, config: ApiNamespaceConfig): void {
this.#userSpec.apiNamespaces.set(name, config)
}

api(this: App, name: string, config: ApiConfig): void {
this.#userSpec.apis.set(name, config)
}

auth(this: App, config: AuthConfig): void {
this.#userSpec.auth = config
}

client(this: App, config: ClientConfig): void {
this.#userSpec.client = config
}

crud(this: App, name: string, config: Crud): void {
this.#userSpec.cruds.set(name, config)
}

db(this: App, config: DbConfig): void {
this.#userSpec.db = config
}

emailSender(this: App, config: EmailSenderConfig): void {
this.#userSpec.emailSender = config
}

job(this: App, name: string, config: JobConfig): void {
this.#userSpec.jobs.set(name, config)
}

page(this: App, name: string, config: PageConfig): PageName {
this.#userSpec.pages.set(name, config)
return name as PageName
}

query(this: App, name: string, config: QueryConfig): void {
this.#userSpec.queries.set(name, config)
}

route(this: App, name: string, config: RouteConfig): void {
this.#userSpec.routes.set(name, config)
}

server(this: App, config: ServerConfig): void {
this.#userSpec.server = config
}

webSocket(this: App, config: WebsocketConfig) {
this.#userSpec.websocket = config
}
}

export type WaspConfig = AppSpec.Wasp

Expand Down Expand Up @@ -96,7 +183,8 @@ export type ScheduleConfig = {
}

type ExecutorOptions = {
// TODO: Type this better
// TODO: Type this better and test it
// rewriting waspc/todoApp should make sure it works
pgBoss: object
}

Expand Down Expand Up @@ -185,93 +273,3 @@ export type UserSpec = {
server?: ServerConfig
websocket?: WebsocketConfig
}

export class App {
private spec: UserSpec

// TODO: I need a secret method to query the spec, let's keep it public for now
getSpec() {
return this.spec
}

constructor(name: string, config: AppConfig) {
this.spec = {
app: { name, config: config },
actions: new Map(),
apiNamespaces: new Map(),
apis: new Map(),
auth: undefined,
client: undefined,
cruds: new Map(),
db: undefined,
emailSender: undefined,
jobs: new Map(),
pages: new Map(),
queries: new Map(),
routes: new Map(),
server: undefined,
websocket: undefined,
}
}

// TODO: Enforce that all methods are covered in compile time
action(this: App, name: string, config: ActionConfig): void {
this.spec.actions.set(name, config)
}

apiNamespace(this: App, name: string, config: ApiNamespaceConfig): void {
this.spec.apiNamespaces.set(name, config)
}

api(this: App, name: string, config: ApiConfig): void {
this.spec.apis.set(name, config)
}

auth(this: App, config: AuthConfig): void {
this.spec.auth = config
}

client(this: App, config: ClientConfig): void {
this.spec.client = config
}

crud(this: App, name: string, config: Crud): void {
this.spec.cruds.set(name, config)
}

db(this: App, config: DbConfig): void {
this.spec.db = config
}

emailSender(this: App, config: EmailSenderConfig): void {
this.spec.emailSender = config
}

job(this: App, name: string, config: JobConfig): void {
this.spec.jobs.set(name, config)
}

page(this: App, name: string, config: PageConfig): PageName {
this.spec.pages.set(name, config)
// NOTE: return a string or return a page object?
return name as PageName
}

query(this: App, name: string, config: QueryConfig): void {
this.spec.queries.set(name, config)
}

route(this: App, name: string, config: RouteConfig): void {
this.spec.routes.set(name, config)
}

// TODO: Learn more about typing 'this'
server(this: App, config: ServerConfig): void {
this.spec.server = config
}

// NOTE: I changed this to lowercase because it's more standard
websocket(this: App, config: WebsocketConfig) {
this.spec.websocket = config
}
}

0 comments on commit 30c3754

Please sign in to comment.