Skip to content

Commit b63121f

Browse files
committed
add apikey repository and services
1 parent be590f9 commit b63121f

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { db } from '../db/database'
2+
import type { ApiKeyCreationParams, SelectableApiKey } from './api-key'
3+
4+
export function createApiKey(projectId: number): Promise<SelectableApiKey> {
5+
const insertKey: ApiKeyCreationParams = {
6+
key: 'something',
7+
project_id: projectId
8+
}
9+
return db
10+
.insertInto('apikeys')
11+
.values(insertKey)
12+
.returningAll()
13+
.executeTakeFirstOrThrow(() => new Error('Error creating Api Access Key'))
14+
}
15+
16+
export function getApiKeysForProject(projectId: number): Promise<SelectableApiKey[]> {
17+
return db.selectFrom('apikeys').selectAll().where('project_id', '==', projectId).execute()
18+
}
19+
20+
export async function projectHasKey(projectId: number, key: string): Promise<boolean> {
21+
const result = await db
22+
.selectFrom('apikeys')
23+
.where('project_id', '==', projectId)
24+
.where('key', '==', key)
25+
.execute()
26+
return !!result.length
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { projectHasKey } from './api-key.repository'
2+
3+
export const checkApiKeyAccess = async (apiKey: string, projectId: number): Promise<boolean> => {
4+
return await projectHasKey(projectId, apiKey)
5+
}

services/src/auth/api-key.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export const checkApiKeyAccess = async (apiKey: string, project: string): Promise<boolean> => {
2-
//TODO implement
3-
return Promise.resolve(false)
4-
}
1+
import type { Insertable, Selectable } from 'kysely'
2+
import type { Apikeys } from 'kysely-codegen'
3+
4+
export type ApiKeyCreationParams = Insertable<Omit<Apikeys, 'id' | 'created_at'>>
5+
export type SelectableApiKey = Selectable<Apikeys>

src/routes/(api)/api/api-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { checkApiKeyAccess } from 'services/auth/api-key'
1+
import { checkApiKeyAccess } from 'services/auth/api-key.service'
22
import type { ProjectId } from './api.model'
33
import { error } from '@sveltejs/kit'
44

src/routes/(api)/api/api.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type TranslationKey = z.infer<typeof translationKeySchema>
66
const translationValueSchema = z.string().brand('translation-value')
77
export type TranslationValue = z.infer<typeof translationValueSchema>
88

9-
const projectIdSchema = z.string().brand('project-id')
9+
const projectIdSchema = z.number().brand('project-id')
1010
export type ProjectId = z.infer<typeof projectIdSchema>
1111

1212
const addTranslationCommandSchema = z.object({

0 commit comments

Comments
 (0)