|
| 1 | +import { |
| 2 | + HttpApi, |
| 3 | + HttpApiEndpoint, |
| 4 | + HttpApiGroup, |
| 5 | + HttpApiSchema, |
| 6 | +} from "@effect/platform"; |
| 7 | +import { Schema } from "effect"; |
| 8 | + |
| 9 | +export const ClientId = Schema.UUID; |
| 10 | +export const WorkspaceId = Schema.UUID; |
| 11 | +export const Scope = Schema.Literal("read", "read_write"); |
| 12 | + |
| 13 | +export class ClientTable extends Schema.Class<ClientTable>("ClientTable")({ |
| 14 | + clientId: ClientId, |
| 15 | + createdAt: Schema.DateFromString, |
| 16 | +}) {} |
| 17 | + |
| 18 | +export class WorkspaceTable extends Schema.Class<WorkspaceTable>( |
| 19 | + "WorkspaceTable" |
| 20 | +)({ |
| 21 | + workspaceId: WorkspaceId, |
| 22 | + ownerClientId: ClientId, |
| 23 | + createdAt: Schema.DateFromString, |
| 24 | + clientId: ClientId, |
| 25 | + snapshot: Schema.Uint8Array, |
| 26 | +}) {} |
| 27 | + |
| 28 | +export class TokenTable extends Schema.Class<TokenTable>("TokenTable")({ |
| 29 | + tokenId: Schema.Number, |
| 30 | + tokenValue: Schema.String, |
| 31 | + clientId: ClientId, |
| 32 | + workspaceId: WorkspaceId, |
| 33 | + isMaster: Schema.Boolean, |
| 34 | + scope: Scope, |
| 35 | + issuedAt: Schema.DateFromString, |
| 36 | + expiresAt: Schema.NullOr(Schema.DateFromString), |
| 37 | + revokedAt: Schema.NullOr(Schema.DateFromString), |
| 38 | +}) {} |
| 39 | + |
| 40 | +export class SyncAuthGroup extends HttpApiGroup.make("syncAuth") |
| 41 | + .add( |
| 42 | + /** |
| 43 | + Allows a client to create a new workshop and upload its initial data to the server. The server marks the client as the owner and issues a master token for full control. |
| 44 | + */ |
| 45 | + HttpApiEndpoint.post("generateToken")`/workspaces` |
| 46 | + .setPayload( |
| 47 | + Schema.Struct({ |
| 48 | + clientId: ClientId, |
| 49 | + workspaceId: WorkspaceId, |
| 50 | + snapshot: WorkspaceTable.fields.snapshot, |
| 51 | + }) |
| 52 | + ) |
| 53 | + .addError(Schema.String) |
| 54 | + .addSuccess(Schema.Struct({ token: Schema.String })) |
| 55 | + ) |
| 56 | + .add( |
| 57 | + /** |
| 58 | + Allows the owner (via master token) to generate an access token for another client, specifying permissions and expiration. The owner shares this token with the client securely. |
| 59 | + */ |
| 60 | + HttpApiEndpoint.post( |
| 61 | + "issueToken" |
| 62 | + )`/workspaces/${HttpApiSchema.param("workspaceId", Schema.UUID)}/token` |
| 63 | + .setPayload( |
| 64 | + Schema.Struct({ |
| 65 | + clientId: ClientId, |
| 66 | + scope: Scope, |
| 67 | + expiresIn: Schema.Duration, |
| 68 | + }) |
| 69 | + ) |
| 70 | + .addError(Schema.String) |
| 71 | + .setHeaders( |
| 72 | + Schema.Struct({ |
| 73 | + Authorization: Schema.String, |
| 74 | + }) |
| 75 | + ) |
| 76 | + .addSuccess( |
| 77 | + Schema.Struct({ |
| 78 | + token: Schema.String, |
| 79 | + scope: Scope, |
| 80 | + expiresAt: Schema.DateFromString, |
| 81 | + }) |
| 82 | + ) |
| 83 | + ) |
| 84 | + .add( |
| 85 | + /** |
| 86 | + Lets the owner revoke access for a specific client by invalidating their access token. Requires the master token and targets the `clientId` tied to the token. |
| 87 | + */ |
| 88 | + HttpApiEndpoint.del( |
| 89 | + "revokeToken" |
| 90 | + )`/workspaces/${HttpApiSchema.param("workspaceId", Schema.UUID)}/token/${HttpApiSchema.param("clientId", Schema.UUID)}` |
| 91 | + .addError(Schema.String) |
| 92 | + .setHeaders( |
| 93 | + Schema.Struct({ |
| 94 | + Authorization: Schema.String, |
| 95 | + }) |
| 96 | + ) |
| 97 | + .addSuccess(Schema.Boolean) |
| 98 | + ) |
| 99 | + .add( |
| 100 | + /** |
| 101 | + Provides the owner with a list of all active tokens (master and access) for a workshop, showing their status. Useful for managing access. |
| 102 | + */ |
| 103 | + HttpApiEndpoint.get( |
| 104 | + "listTokens" |
| 105 | + )`/workspaces/${HttpApiSchema.param("workspaceId", Schema.UUID)}/tokens` |
| 106 | + .addError(Schema.String) |
| 107 | + .setHeaders( |
| 108 | + Schema.Struct({ |
| 109 | + Authorization: Schema.String, |
| 110 | + }) |
| 111 | + ) |
| 112 | + .addSuccess( |
| 113 | + Schema.Array( |
| 114 | + TokenTable.pipe( |
| 115 | + Schema.pick( |
| 116 | + "clientId", |
| 117 | + "tokenValue", |
| 118 | + "scope", |
| 119 | + "isMaster", |
| 120 | + "issuedAt", |
| 121 | + "expiresAt", |
| 122 | + "revokedAt" |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) {} |
| 128 | + |
| 129 | +export class SyncDataGroup extends HttpApiGroup.make("syncData") |
| 130 | + .add( |
| 131 | + /** |
| 132 | + Updates the workshop data on the server with changes from a client. Requires a valid token with `read_write` scope. |
| 133 | + */ |
| 134 | + HttpApiEndpoint.put( |
| 135 | + "push" |
| 136 | + )`/workspaces/${HttpApiSchema.param("workspaceId", Schema.UUID)}/sync` |
| 137 | + .setPayload(WorkspaceTable.pipe(Schema.pick("clientId", "snapshot"))) |
| 138 | + .addError(Schema.String) |
| 139 | + .setHeaders( |
| 140 | + Schema.Struct({ |
| 141 | + Authorization: Schema.String, |
| 142 | + }) |
| 143 | + ) |
| 144 | + .addSuccess( |
| 145 | + WorkspaceTable.pipe(Schema.pick("workspaceId", "createdAt", "snapshot")) |
| 146 | + ) |
| 147 | + ) |
| 148 | + .add( |
| 149 | + /** |
| 150 | + Retrieves the current workshop data for a client (owner or authorized user). Requires a valid token (master or access) with at least `read` scope. Used for initial download or sync verification. |
| 151 | + */ |
| 152 | + HttpApiEndpoint.get( |
| 153 | + "pull" |
| 154 | + )`/workspaces/${HttpApiSchema.param("workspaceId", Schema.UUID)}` |
| 155 | + .addError(Schema.String) |
| 156 | + .setHeaders( |
| 157 | + Schema.Struct({ |
| 158 | + Authorization: Schema.String, |
| 159 | + }) |
| 160 | + ) |
| 161 | + .addSuccess( |
| 162 | + WorkspaceTable.pipe(Schema.pick("workspaceId", "createdAt", "snapshot")) |
| 163 | + ) |
| 164 | + ) {} |
| 165 | + |
| 166 | +export class SyncApi extends HttpApi.make("SyncApi") |
| 167 | + .add(SyncAuthGroup) |
| 168 | + .add(SyncDataGroup) {} |
0 commit comments