Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play with vertical slice 2 #13

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .vscode/model.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ResourceCodegen": {
"prefix": "genrsc",
"body": [
"// codegen:start {preset: meta, sourcePrefix: src/resources/}"
"// codegen:start {preset: meta, sourcePrefix: src/}"
]
},
"ModelComplete": {
Expand Down
15 changes: 0 additions & 15 deletions api/src/Accounts.controllers.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { RepoConfig } from "#api/config"
import { RepoDefault } from "#api/lib/layers"
import type { UserId } from "#models/User"
import { User } from "#models/User"
import { Model } from "@effect-app/infra"
import { NotFoundError, NotLoggedInError } from "@effect-app/infra/errors"
import { Q } from "@effect-app/infra/Model"
import { generate } from "@effect-app/infra/test"
import { Array, Effect, Exit, Layer, Option, pipe, Request, RequestResolver, S } from "effect-app"
import { fakerArb } from "effect-app/faker"
import { Email } from "effect-app/Schema"
import fc from "fast-check"
import { Q } from "../lib.js"
import { UserProfile } from "../UserProfile.js"
import type { UserId } from "./models.js"
import { User } from "./models.js"

export type UserSeed = "sample" | ""

Expand Down
15 changes: 8 additions & 7 deletions api/src/Users.controllers.ts → api/src/Accounts/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { matchFor, Router } from "#api/lib/routing"
import { Q, UserRepo } from "#api/services"
import { UsersRsc } from "#resources"
import type { UserView } from "#resources/views"
import { Array } from "effect"
import { Effect, Order } from "effect-app"
import { Q } from "@effect-app/infra/Model"
import { Array, Effect, Order } from "effect-app"
import type { UserView } from "./resources.js"
import { AccountsRsc } from "./resources.js"
import { UserRepo } from "./UserRepo.js"

export default Router(UsersRsc)({
export default Router(AccountsRsc)({
dependencies: [UserRepo.Default],
effect: Effect.gen(function*() {
const userRepo = yield* UserRepo

return matchFor(UsersRsc)({
return matchFor(AccountsRsc)({
GetMe: userRepo.getCurrentUser,
IndexUsers: (req) =>
userRepo
.query(Q.where("id", "in", req.filterByIds))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { UserId } from "#models/User"
import { clientFor } from "#resources/lib"
import { Effect, Exit, Request, RequestResolver } from "effect"
import { Array, Option, pipe, S } from "effect-app"
import { ApiClientFactory, NotFoundError } from "effect-app/client"
import { type Schema } from "effect-app/Schema"
import * as UsersRsc from "../Users.js"
import { UserView } from "../views/UserView.js"
import { UserId } from "./models.js"
import { AccountsRsc, UserView } from "./resources.js"

interface GetUserViewById extends Request.Request<UserView, NotFoundError<"User">> {
readonly _tag: "GetUserViewById"
Expand All @@ -15,7 +14,7 @@ const GetUserViewById = Request.tagged<GetUserViewById>("GetUserViewById")

const getUserViewByIdResolver = RequestResolver
.makeBatched((requests: GetUserViewById[]) =>
clientFor(UsersRsc).pipe(
clientFor(AccountsRsc).pipe(
Effect.flatMap((client) =>
client
.IndexUsers
Expand Down
37 changes: 37 additions & 0 deletions api/src/Accounts/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { S } from "#resources/lib"
import { NotFoundError } from "effect-app/client"
import { User, UserId } from "./models.js"

export class UserView extends S.ExtendedClass<UserView, UserView.Encoded>()({
...User.pick("id", "role"),
displayName: S.NonEmptyString2k
}) {}

export class GetMe extends S.Req<GetMe>()("GetMe", {}, { success: User, failure: NotFoundError }) {}

export class IndexUsers extends S.Req<IndexUsers>()("IndexUsers", {
filterByIds: S.NonEmptyArray(UserId)
}, {
allowAnonymous: true,
allowRoles: ["user"],
success: S.Struct({
users: S.Array(UserView)
})
}) {}

// codegen:start {preset: meta, sourcePrefix: src/}
export const meta = { moduleName: "Accounts" } as const
// codegen:end

// codegen:start {preset: model}
//
/* eslint-disable */
export namespace UserView {
export interface Encoded extends S.Struct.Encoded<typeof UserView["fields"]> {}
}
/* eslint-enable */
//
// codegen:end
//

export * as AccountsRsc from "./resources.js"
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { UserFromIdResolver } from "#Accounts/models"
import { UserRepo } from "#Accounts/UserRepo"
import { RepoDefault } from "#api/lib/layers"
import { BlogPost } from "#models/Blog"
import { UserFromIdResolver } from "#models/User"
import { Model } from "@effect-app/infra"
import { Effect } from "effect"
import { Context } from "effect-app"
import { NonEmptyString255, NonEmptyString2k } from "effect-app/Schema"
import { UserRepo } from "./UserRepo.js"
import { BlogPost } from "./models.js"

export type BlogPostSeed = "sample" | ""

Expand Down
11 changes: 7 additions & 4 deletions api/src/Blog.controllers.ts → api/src/Blog/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { UserRepo } from "#api/Accounts/UserRepo"
import { Events } from "#api/Events"
import { matchFor, Router } from "#api/lib/routing"
import { BlogPostRepo, Events, Operations, UserRepo } from "#api/services"
import { BlogPost } from "#models/Blog"
import { BlogRsc } from "#resources"
import { BogusEvent } from "#resources/Events"
import { Operations } from "@effect-app/infra/Operations"
import { Duration, Effect, Schedule } from "effect"
import { Option } from "effect-app"
import { NonEmptyString2k, NonNegativeInt } from "effect-app/Schema"
import { OperationsDefault } from "./lib/layers.js"
import { OperationsDefault } from "../lib/layers.js"
import { BlogPostRepo } from "./BlogPostRepo.js"
import { BlogPost } from "./models.js"
import { BlogRsc } from "./resources.js"

export default Router(BlogRsc)({
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion api/src/models/Blog.ts → api/src/Blog/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserFromId } from "#Accounts/models"
import { S } from "effect-app"
import { UserFromId } from "./User.js"

export const BlogPostId = S.prefixedStringId<BlogPostId>()("post", "BlogPostId")
export interface BlogPostIdBrand {
Expand Down
25 changes: 21 additions & 4 deletions api/src/resources/Blog.ts → api/src/Blog/resources.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { BlogPost, BlogPostId } from "#models/Blog"
import { UserViewFromId } from "#api/Accounts/resolvers"
import { S } from "#resources/lib"
import { InvalidStateError, NotFoundError, OptimisticConcurrencyException } from "effect-app/client"
import { OperationId } from "effect-app/Operations"
import { S } from "./lib.js"
import { BlogPostView } from "./views.js"
import { BlogPost, BlogPostId } from "./models.js"

export class BlogPostView extends S.ExtendedClass<BlogPostView, BlogPostView.Encoded>()({
...BlogPost.omit("author"),
author: S.propertySignature(UserViewFromId).pipe(S.fromKey("authorId"))
}) {}

export class CreatePost extends S.Req<CreatePost>()("CreatePost", BlogPost.pick("title", "body"), {
allowRoles: ["user"],
Expand All @@ -26,6 +31,18 @@ export class PublishPost extends S.Req<PublishPost>()("PublishPost", {
id: BlogPostId
}, { allowRoles: ["user"], success: OperationId, failure: S.Union(NotFoundError) }) {}

// codegen:start {preset: meta, sourcePrefix: src/resources/}
// codegen:start {preset: meta, sourcePrefix: src/}
export const meta = { moduleName: "Blog" } as const
// codegen:end

// codegen:start {preset: model}
//
/* eslint-disable */
export namespace BlogPostView {
export interface Encoded extends S.Struct.Encoded<typeof BlogPostView["fields"]> {}
}
/* eslint-enable */
//
// codegen:end

export * as BlogRsc from "./resources.js"
2 changes: 1 addition & 1 deletion api/src/services/Events.ts → api/src/Events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ClientEvents } from "#resources"
import { storeId } from "@effect-app/infra/Store/Memory"
import { Effect, FiberRef, PubSub, Stream } from "effect-app"
import type { NonEmptyReadonlyArray } from "effect/Array"
import type { ClientEvents } from "./resources/Events.js"

export class Events extends Effect.Service<Events>()("Events", {
accessors: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { User } from "#api/Accounts/models"
import { UserRepo } from "#api/Accounts/UserRepo"
import { matchFor, Router } from "#api/lib/routing"
import { UserRepo } from "#api/services"
import { User } from "#models/User"
import { HelloWorldRsc } from "#resources"
import { GetHelloWorld } from "#resources/HelloWorld"
import { getRequestContext } from "@effect-app/infra/api/setupRequest"
import { generate } from "@effect-app/infra/test"
import { Effect, S } from "effect-app"
import { GetHelloWorld, HelloWorldRsc } from "./resources.js"

export default Router(HelloWorldRsc)({
dependencies: [UserRepo.Default],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserView } from "#api/Accounts/resources"
import { S } from "#resources/lib"
import { RequestContext } from "@effect-app/infra/RequestContext"
import { S } from "./lib.js"
import { UserView } from "./views.js"

class Response extends S.Class<Response>()({
now: S.Date.withDefault,
Expand All @@ -14,6 +14,7 @@ export class GetHelloWorld extends S.Req<GetHelloWorld>()("GetHelloWorld", {
echo: S.String
}, { allowAnonymous: true, allowRoles: ["user"], success: Response }) {}

// codegen:start {preset: meta, sourcePrefix: src/resources/}
// codegen:start {preset: meta, sourcePrefix: src/}
export const meta = { moduleName: "HelloWorld" } as const
// codegen:end
export * as HelloWorldRsc from "./resources.js"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OperationsDefault } from "#api/lib/layers"
import { matchFor, Router } from "#api/lib/routing"
import { Operations } from "#api/services"
import { OperationsRsc } from "#resources"
import { Operations } from "@effect-app/infra/Operations"
import { Effect } from "effect-app"
import { OperationsDefault } from "./lib/layers.js"
import { OperationsRsc } from "./resources.js"

export default Router(OperationsRsc)({
dependencies: [OperationsDefault],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { clientFor, S } from "#api/resources/lib"
import { Duration, Effect } from "effect-app"
import { NotFoundError } from "effect-app/client"
import { Operation, OperationFailure, OperationId } from "effect-app/Operations"
import { clientFor } from "./lib.js"
import * as S from "./lib/schema.js"

export class FindOperation extends S.Req<FindOperation>()("FindOperation", {
id: OperationId
}, { allowAnonymous: true, allowRoles: ["user"], success: S.NullOr(Operation) }) {}

// codegen:start {preset: meta, sourcePrefix: src/resources/}
// codegen:start {preset: meta, sourcePrefix: src/}
export const meta = { moduleName: "Operations" } as const
// codegen:end

Expand Down Expand Up @@ -96,3 +95,5 @@ export const OperationsClient = Effect.gen(function*() {
waitForOperation_
})
})

export * as OperationsRsc from "./resources.js"
2 changes: 1 addition & 1 deletion api/src/services/UserProfile.ts → api/src/UserProfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Role } from "#models/User"
import { parseJwt } from "@effect-app/infra/api/routing/schema/jwt"
import { Context, S } from "effect-app"
import { UserProfileId } from "effect-app/ids"
import { Role } from "./Accounts/models.js"

export class UserProfile extends Context.assignTag<UserProfile>()(
S.Class<UserProfile>()({
Expand Down
13 changes: 6 additions & 7 deletions api/src/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// codegen:start {preset: barrel, include: ./*.controllers.ts, import: default}
import accountsControllers from "./Accounts.controllers.js"
import blogControllers from "./Blog.controllers.js"
import helloWorldControllers from "./HelloWorld.controllers.js"
import operationsControllers from "./Operations.controllers.js"
import usersControllers from "./Users.controllers.js"
// codegen:start {preset: barrel, include: ./*/controllers.ts, import: default}
import accountsControllers from "./Accounts/controllers.js"
import blogControllers from "./Blog/controllers.js"
import helloWorldControllers from "./HelloWorld/controllers.js"
import operationsControllers from "./Operations/controllers.js"

export { accountsControllers, blogControllers, helloWorldControllers, operationsControllers, usersControllers }
export { accountsControllers, blogControllers, helloWorldControllers, operationsControllers }
// codegen:end
4 changes: 2 additions & 2 deletions api/src/lib/middleware/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Events } from "#api/services"
import { ClientEvents } from "#resources"
import { Events } from "#api/Events"
import { ClientEvents } from "#api/resources/Events"
import { makeSSE } from "@effect-app/infra/api/middlewares"
import { Effect } from "effect-app"

Expand Down
6 changes: 1 addition & 5 deletions api/src/lib/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import { Context, Effect, Exit, Layer, Option } from "effect-app"
import type { GetEffectContext, RPCContextMap } from "effect-app/client"
import type { HttpHeaders, HttpServerRequest } from "effect-app/http"
import type * as EffectRequest from "effect/Request"
import {
makeUserProfileFromAuthorizationHeader,
makeUserProfileFromUserHeader,
UserProfile
} from "../services/UserProfile.js"
import { makeUserProfileFromAuthorizationHeader, makeUserProfileFromUserHeader, UserProfile } from "../UserProfile.js"
import { basicRuntime } from "./basicRuntime.js"

export interface CTX {
Expand Down
11 changes: 0 additions & 11 deletions api/src/resources.ts

This file was deleted.

9 changes: 0 additions & 9 deletions api/src/resources/Accounts.ts

This file was deleted.

17 changes: 0 additions & 17 deletions api/src/resources/Users.ts

This file was deleted.

2 changes: 1 addition & 1 deletion api/src/resources/lib/req.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Role } from "#models/User"
import type { Role } from "#api/Accounts/models"
import { NotLoggedInError, UnauthorizedError } from "@effect-app/infra/errors"
import { Duration, Layer, Request as EffectRequest } from "effect-app"
import type { RPCContextMap } from "effect-app/client"
Expand Down
4 changes: 0 additions & 4 deletions api/src/resources/views.ts

This file was deleted.

Loading