-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from tscircuit/registry-compat
Registry Compatibility, Login
- Loading branch information
Showing
46 changed files
with
760 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Created using @tscircuit/plop (npm install -g @tscircuit/plop) | ||
name: Format Check | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
format-check: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup bun | ||
uses: oven-sh/setup-bun@v1 | ||
with: | ||
bun-version: latest | ||
|
||
- name: Install dependencies | ||
run: bun install | ||
|
||
- name: Run format check | ||
run: bun run lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Created using @tscircuit/plop (npm install -g @tscircuit/plop) | ||
name: Type Check | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
type-check: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup bun | ||
uses: oven-sh/setup-bun@v1 | ||
with: | ||
bun-version: latest | ||
|
||
- name: Install dependencies | ||
run: bun i | ||
|
||
- name: Run type check | ||
run: bunx tsc --noEmit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,47 @@ | ||
import { z } from "zod" | ||
|
||
export const snippetSchema = z | ||
.object({ | ||
snippet_id: z.string(), | ||
full_snippet_name: z.string(), | ||
snippet_name: z.string(), | ||
owner_name: z.string(), | ||
content: z.string(), | ||
created_at: z.string(), | ||
updated_at: z.string(), | ||
is_board: z.boolean().default(true), | ||
is_package: z.boolean().default(false), | ||
is_model: z.boolean().default(false), | ||
is_footprint: z.boolean().default(false), | ||
}) | ||
.transform((snippet) => ({ | ||
...snippet, | ||
type: snippet.is_board | ||
? ("board" as const) | ||
: snippet.is_package | ||
? ("package" as const) | ||
: snippet.is_model | ||
? ("model" as const) | ||
: snippet.is_footprint | ||
? ("footprint" as const) | ||
: ("board" as const), | ||
})) | ||
export const snippetSchema = z.object({ | ||
snippet_id: z.string(), | ||
name: z.string(), | ||
unscoped_name: z.string(), | ||
owner_name: z.string(), | ||
code: z.string(), | ||
created_at: z.string(), | ||
updated_at: z.string(), | ||
snippet_type: z.enum(["board", "package", "model", "footprint"]), | ||
description: z.string().optional(), | ||
}) | ||
export type Snippet = z.infer<typeof snippetSchema> | ||
|
||
export const sessionSchema = z.object({ | ||
session_id: z.string(), | ||
account_id: z.string(), | ||
expires_at: z.string(), | ||
is_cli_session: z.boolean(), | ||
}) | ||
export type Session = z.infer<typeof sessionSchema> | ||
|
||
export const loginPageSchema = z.object({ | ||
login_page_id: z.string(), | ||
login_page_auth_token: z.string(), | ||
was_login_successful: z.boolean(), | ||
has_been_used_to_create_session: z.boolean(), | ||
created_at: z.string(), | ||
expires_at: z.string(), | ||
}) | ||
export type LoginPage = z.infer<typeof loginPageSchema> | ||
|
||
export const accountSchema = z.object({ | ||
account_id: z.string(), | ||
github_username: z.string(), | ||
}) | ||
export type Account = z.infer<typeof accountSchema> | ||
|
||
export const databaseSchema = z.object({ | ||
idCounter: z.number().default(0), | ||
snippets: z.array(snippetSchema).default([]), | ||
sessions: z.array(sessionSchema).default([]), | ||
loginPages: z.array(loginPageSchema).default([]), | ||
accounts: z.array(accountSchema).default([]), | ||
}) | ||
export type DatabaseSchema = z.infer<typeof databaseSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Middleware } from "winterspec/middleware" | ||
|
||
export type CtxErrorFn = ( | ||
status: number, | ||
error_payload: { | ||
error_code: string | ||
message: string | ||
}, | ||
) => Response | ||
|
||
export const withCtxError: Middleware< | ||
{}, | ||
{ | ||
error: CtxErrorFn | ||
} | ||
> = async (req, ctx, next) => { | ||
ctx.error = (status, error_payload) => { | ||
return new Response(JSON.stringify({ error: error_payload }), { | ||
status, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
} | ||
return next(req, ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Middleware } from "winterspec/middleware" | ||
import { CtxErrorFn } from "./with-ctx-error" | ||
|
||
export const withSessionAuth: Middleware< | ||
{ | ||
error: CtxErrorFn | ||
}, | ||
{ | ||
auth: { | ||
type: "session" | ||
account_id: string | ||
personal_org_id: string | ||
github_username: string | ||
session_id: string | ||
} | ||
}, | ||
{} | ||
> = async (req, ctx, next) => { | ||
if (req.method === "OPTIONS") return next(req, ctx) | ||
|
||
const token = req.headers.get("authorization")?.split("Bearer ")?.[1] | ||
|
||
console.log("token", token) | ||
|
||
if (!token) { | ||
return ctx.error(401, { | ||
error_code: "no_token", | ||
message: "No token provided", | ||
}) | ||
} | ||
|
||
ctx.auth = { | ||
type: "session", | ||
account_id: "account-1234", | ||
personal_org_id: "org-1234", | ||
github_username: "testuser", | ||
session_id: "session-1234", | ||
} | ||
|
||
return next(req, ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
fake-snippets-api/routes/api/internal/sessions/create_without_auth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { withRouteSpec } from "fake-snippets-api/lib/middleware/with-winter-spec" | ||
import { z } from "zod" | ||
import ms from "ms" | ||
import { SignJWT } from "jose" | ||
|
||
export default withRouteSpec({ | ||
methods: ["POST"], | ||
auth: "none", | ||
jsonBody: z | ||
.object({ | ||
account_id: z.string(), | ||
}) | ||
.or( | ||
z.object({ | ||
github_username: z.string(), | ||
}), | ||
), | ||
jsonResponse: z.object({ | ||
session: z.object({ | ||
session_id: z.string(), | ||
account_id: z.string(), | ||
expires_at: z.string(), | ||
is_cli_session: z.boolean(), | ||
token: z.string(), | ||
}), | ||
}), | ||
})(async (req, ctx) => { | ||
let account | ||
if ("account_id" in req.jsonBody) { | ||
account = ctx.db.getAccount(req.jsonBody.account_id) | ||
} else { | ||
account = ctx.db.getAccount(req.jsonBody.github_username) | ||
} | ||
|
||
if (!account) { | ||
return ctx.error(404, { | ||
error_code: "account_not_found", | ||
message: "Account not found", | ||
}) | ||
} | ||
|
||
const new_session = ctx.db.createSession({ | ||
expires_at: new Date(Date.now() + ms("60 day")).toISOString(), | ||
account_id: account.account_id, | ||
is_cli_session: false, | ||
}) | ||
|
||
const token = await new SignJWT({ | ||
account_id: account.account_id, | ||
session_id: new_session.session_id, | ||
github_username: account.github_username, | ||
}) | ||
.setProtectedHeader({ alg: "HS256" }) | ||
.setExpirationTime(new Date(Date.now() + ms("60 day")).toISOString()) | ||
.sign(new TextEncoder().encode(process.env.JWT_SECRET || "")) | ||
|
||
return ctx.json({ | ||
session: { | ||
...new_session, | ||
token, | ||
}, | ||
}) | ||
}) |
Oops, something went wrong.