Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/tidy-mice-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kivotos/core": minor
---

[[DRIZZ-67] Permission](https://app.plane.so/softnetics/browse/DRIZZ-67)
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "./src/index.ts",
"module": "./src/index.ts",
"exports": {
".": "./src/index.ts"
".": "./src/index.ts",
"./plugins/*": "./src/plugins/*/index.ts"
},
"scripts": {
"lint": "eslint .",
Expand All @@ -17,6 +18,7 @@
},
"dependencies": {
"cookie-es": "^2.0.0",
"deepmerge-ts": "^7.1.5",
"drizzle-orm": "^0.41.0",
"remeda": "^2.21.2",
"valibot": "^1.0.0",
Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/__mocks__/complex-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import {
varchar,
} from 'drizzle-orm/pg-core'

export const users = pgTable('users', {
export const user = pgTable('user', {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
emailVerified: boolean('email_verified').notNull().default(false),
image: text('image'),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
role: text('role').notNull().default('user'),
banned: boolean('banned').default(false),
bannedReason: text('banned_reason'),
bannedExpiresAt: timestamp('banned_expires_at'),
})

export const sessions = pgTable('session', {
export const session = pgTable('session', {
id: uuid('id').primaryKey().defaultRandom(),
expiresAt: timestamp('expires_at').notNull(),
token: text('token').notNull().unique(),
Expand All @@ -30,16 +34,16 @@ export const sessions = pgTable('session', {
userAgent: text('user_agent'),
userId: text('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
.references(() => user.id, { onDelete: 'cascade' }),
})

export const accounts = pgTable('account', {
export const account = pgTable('account', {
id: uuid('id').primaryKey().defaultRandom(),
accountId: text('account_id').notNull(),
providerId: text('provider_id').notNull(),
userId: text('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
.references(() => user.id, { onDelete: 'cascade' }),
accessToken: text('access_token'),
refreshToken: text('refresh_token'),
idToken: text('id_token'),
Expand All @@ -51,7 +55,7 @@ export const accounts = pgTable('account', {
updatedAt: timestamp('updated_at').notNull(),
})

export const verifications = pgTable('verification', {
export const verification = pgTable('verification', {
id: uuid('id').primaryKey().defaultRandom(),
identifier: text('identifier').notNull(),
value: text('value').notNull(),
Expand Down
86 changes: 49 additions & 37 deletions packages/core/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,55 @@ import type { ApiRouteHandler } from '../endpoint'
import type { Fields, FieldsClient } from '../field'
import type { AnyTypedColumn, WithAnyTable, WithHasDefault, WithNotNull } from '../table'

export type AnyUserTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
name: WithNotNull<AnyTypedColumn<string>>
email: WithNotNull<AnyTypedColumn<string>>
emailVerified: WithNotNull<AnyTypedColumn<boolean>>
image: AnyTypedColumn<string>
}>

export type AnySessionTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
token: WithNotNull<AnyTypedColumn<string>>
ipAddress: AnyTypedColumn<string>
userAgent: AnyTypedColumn<string>
userId: WithNotNull<AnyTypedColumn<string>>
}>

export type AnyAccountTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
accountId: WithNotNull<AnyTypedColumn<string>>
providerId: WithNotNull<AnyTypedColumn<string>>
userId: WithNotNull<AnyTypedColumn<string>>
accessToken: AnyTypedColumn<string>
refreshToken: AnyTypedColumn<string>
idToken: AnyTypedColumn<string>
accessTokenExpiresAt: AnyTypedColumn<Date>
refreshTokenExpiresAt: AnyTypedColumn<Date>
scope: AnyTypedColumn<string>
password: AnyTypedColumn<string>
}>

export type AnyVerificationTable = WithAnyTable<{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
identifier: WithNotNull<AnyTypedColumn<string>>
value: AnyTypedColumn<string>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
}>
export type AnyUserTable = WithAnyTable<
{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
name: WithNotNull<AnyTypedColumn<string>>
email: WithNotNull<AnyTypedColumn<string>>
emailVerified: WithNotNull<AnyTypedColumn<boolean>>
image: AnyTypedColumn<string>
},
'user'
>

export type AnySessionTable = WithAnyTable<
{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
token: WithNotNull<AnyTypedColumn<string>>
ipAddress: AnyTypedColumn<string>
userAgent: AnyTypedColumn<string>
userId: WithNotNull<AnyTypedColumn<string>>
},
'session'
>

export type AnyAccountTable = WithAnyTable<
{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
accountId: WithNotNull<AnyTypedColumn<string>>
providerId: WithNotNull<AnyTypedColumn<string>>
userId: WithNotNull<AnyTypedColumn<string>>
accessToken: AnyTypedColumn<string>
refreshToken: AnyTypedColumn<string>
idToken: AnyTypedColumn<string>
accessTokenExpiresAt: AnyTypedColumn<Date>
refreshTokenExpiresAt: AnyTypedColumn<Date>
scope: AnyTypedColumn<string>
password: AnyTypedColumn<string>
},
'account'
>

export type AnyVerificationTable = WithAnyTable<
{
id: WithHasDefault<WithNotNull<AnyTypedColumn<string>>>
identifier: WithNotNull<AnyTypedColumn<string>>
value: AnyTypedColumn<string>
expiresAt: WithNotNull<AnyTypedColumn<Date>>
},
'verification'
>

export interface AuthConfig {
secret: string
Expand Down
26 changes: 17 additions & 9 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type MinimalContext<
TContext & {
db: NodePgDatabase<TFullSchema>
}
>
> & {}

export interface BaseConfigOptions<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
Expand Down Expand Up @@ -70,6 +70,14 @@ export interface ServerConfig<
endpoints: TApiRouter
}

export interface AnyServerConfig
extends ServerConfig<
Record<string, unknown>,
MinimalContext<Record<string, unknown>, {}>,
{},
{}
> {}

export type InferApiRouterFromServerConfig<TServerConfig extends ServerConfig<any, any, any, any>> =
TServerConfig extends ServerConfig<any, any, any, infer TApiRouter>
? TApiRouter extends ApiRouter<any>
Expand All @@ -78,8 +86,8 @@ export type InferApiRouterFromServerConfig<TServerConfig extends ServerConfig<an
: never

export function defineBaseConfig<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends Record<string, unknown> = Record<string, unknown>,
const TFullSchema extends Record<string, unknown> = Record<string, unknown>,
const TContext extends Record<string, unknown> = Record<string, unknown>,
>(
config: BaseConfigOptions<TFullSchema, TContext>
): BaseConfig<TFullSchema, MinimalContext<TFullSchema, TContext>> {
Expand All @@ -95,14 +103,14 @@ export function defineBaseConfig<
}

export function defineServerConfig<
TFullSchema extends Record<string, unknown> = Record<string, unknown>,
TContext extends MinimalContext<TFullSchema> = MinimalContext<TFullSchema>,
const TFullSchema extends Record<string, unknown> = Record<string, unknown>,
const TContext extends MinimalContext<TFullSchema> = MinimalContext<TFullSchema>,
const TCollections extends Record<string, Collection<any, any, any, any, any, any>> = Record<
string,
Collection<any, any, any, any, any, any>
>,
const TEndpoints extends ApiRouter<MinimalContext<TFullSchema, TContext>> = {},
const TPlugins extends KivotosPlugin<any>[] = [],
const TPlugins extends KivotosPlugin<any>[] = [...KivotosPlugin<any>[]],
>(
baseConfig: BaseConfig<TFullSchema, TContext>,
config: { collections: TCollections; endpoints?: TEndpoints; plugins?: TPlugins }
Expand All @@ -118,21 +126,21 @@ export function defineServerConfig<
...auth.handlers,
...collectionEndpoints,
} as TEndpoints &
typeof auth.handlers &
AuthHandlers &
ExtractAllCollectionCustomEndpoints<TCollections> &
ExtractAllCollectionDefaultEndpoints<TCollections>,
} satisfies ServerConfig<
TFullSchema,
MinimalContext<TFullSchema, TContext>,
TCollections,
TEndpoints &
typeof auth.handlers &
AuthHandlers &
ExtractAllCollectionCustomEndpoints<TCollections> &
ExtractAllCollectionDefaultEndpoints<TCollections>
>

for (const plugin of config.plugins ?? []) {
serverConfig = plugin(serverConfig)
serverConfig = plugin.plugin(serverConfig) as any
}
Comment thread
saenyakorn marked this conversation as resolved.
Outdated

return serverConfig as MergePlugins<typeof serverConfig, TPlugins>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export type FieldsClient<TFields extends FieldsClientInitial = FieldsClientIniti
FieldsWithFieldName<TFields>

export type FieldColumnOptionsFromTable<
TColumn extends Column,
TColumn extends Column<any>,
TContext extends Record<string, unknown> = Record<string, unknown>,
> = TColumn['_']['dataType'] extends 'string'
? FieldColumnStringCollectionOptions<TContext>[keyof FieldColumnStringCollectionOptions<TContext>]
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { AuthHandlers } from './auth/handlers'
export type { AuthHandlers } from './auth'
export { Builder } from './builder'
export type {
ApiReturnType,
Expand Down
Loading
Loading