Skip to content

Commit 1c72b19

Browse files
committed
continue
1 parent b0b9d90 commit 1c72b19

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
import { projectHasKey } from './api-key.repository'
1+
import {
2+
createApiKey,
3+
getApiKeysForProject,
4+
projectHasKey,
5+
setApiKeyName
6+
} from './api-key.repository'
27

38
export const checkApiKeyAccess = async (apiKey: string, projectId: number): Promise<boolean> => {
49
return await projectHasKey(projectId, apiKey)
510
}
11+
12+
export const addApiKey = async (projectId: number): Promise<string> => {
13+
const key = await createApiKey(projectId)
14+
return key.key
15+
}
16+
17+
export const changeApiKeyName = async (apiKeyId: number, name: string) => {
18+
await setApiKeyName(apiKeyId, name)
19+
}
20+
21+
export const listApiKeys = async (projectId: number): Promise<string[]> => {
22+
const result = await getApiKeysForProject(projectId)
23+
return result.map((it) => it.key)
24+
}

services/src/auth/api-key.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
import type { Insertable, Selectable } from 'kysely'
22
import type { Apikeys } from 'kysely-codegen'
3+
import { z } from 'zod'
34

45
export type ApiKeyCreationParams = Insertable<Omit<Apikeys, 'id' | 'created_at'>>
56
export type SelectableApiKey = Selectable<Apikeys>
7+
8+
const apiKeyIdSchema = z.number().brand('apiKeyId')
9+
export type ApiKeyId = z.infer<typeof apiKeyIdSchema>
10+
11+
const apiKeyKeySchema = z.string().uuid().brand('apiKeyKey')
12+
export type ApiKeyKey = z.infer<typeof apiKeyKeySchema>
13+
14+
const apiKeySchema = z.object({
15+
id: apiKeyIdSchema,
16+
key: apiKeyKeySchema,
17+
name: z.string(),
18+
project_id: z.number(),
19+
created_at: z.date(),
20+
updated_at: z.date()
21+
})
22+
export type ApiKey = z.infer<typeof apiKeySchema>
23+
24+
const frontendApiKeySchema = apiKeySchema.omit({
25+
id: true
26+
})

services/src/project/project.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import type { Insertable, Selectable } from 'kysely'
22
import type { Projects } from 'kysely-codegen'
3+
import { z } from 'zod'
34

45
export type PojectCreationParams = Insertable<
56
Omit<Projects, 'id' | 'created_at' | 'updated_at' | 'base_language'>
67
>
78
export type SelectableProject = Selectable<Projects>
9+
10+
const projectIdSchema = z.number().brand('projectId')
11+
export type ProjectId = z.infer<typeof projectIdSchema>
12+
13+
const projectSchema = z.object({
14+
id: projectIdSchema,
15+
created_at: z.string(),
16+
updated_at: z.string(),
17+
name: z.string(),
18+
base_language: z.number().nullable()
19+
})
20+
export type Project = z.infer<typeof projectSchema>

0 commit comments

Comments
 (0)