Skip to content

Commit 1205e5a

Browse files
chore: wip
1 parent 0a284f8 commit 1205e5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5338
-4236
lines changed

app/Middleware/Api.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
import { log } from '@stacksjs/cli'
21
import { Middleware } from '@stacksjs/router'
2+
import { request } from '@stacksjs/router'
3+
import { AccessToken, Team } from '../../storage/framework/orm/src'
34

45
export default new Middleware({
56
name: 'API Authentication',
67
priority: 1,
7-
handle() {
8-
log.info('API Authentication')
8+
async handle() {
9+
const bearerToken = request.bearerToken() || ''
10+
11+
const parts = bearerToken.split('|');
12+
13+
// Ensure that the input string contains exactly one '|'
14+
if (parts.length !== 2) {
15+
throw { message: 'Invalid bearer token format', status: 401 }
16+
}
17+
18+
const teamToken = parts[0]
19+
const plainString = parts[1] as string
20+
21+
const team = await Team.find(Number(teamToken))
22+
23+
// if (! team) {
24+
// throw { message: 'Invalid bearer token', status: 401 }
25+
// }
26+
27+
const teamBearerToken = await AccessToken.where('token', plainString)
28+
29+
console.log(teamBearerToken.length)
930
},
1031
})

bun.lockb

-256 Bytes
Binary file not shown.

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async function writeModelRequest() {
134134

135135
for (const attribute of attributes) {
136136
let defaultValue: any = `''`
137-
const entity = attribute.fieldArray?.entity === 'enum' ? 'string' : attribute.fieldArray?.entity
137+
const entity = attribute.fieldArray?.entity === 'enum' ? 'string[]' : attribute.fieldArray?.entity
138138

139139
if (attribute.fieldArray?.entity === 'boolean') defaultValue = false
140140

@@ -649,6 +649,7 @@ async function generateModelString(
649649
const pivotTableRelation = relation.pivotTable
650650
const formattedModelRelation = modelRelation.toLowerCase()
651651
const capitalizeTableRelation = tableRelation.charAt(0).toUpperCase() + tableRelation.slice(1)
652+
652653

653654
const relationType = getRelationType(relation.relationship)
654655
const relationCount = getRelationCount(relation.relationship)
@@ -758,7 +759,11 @@ async function generateModelString(
758759
}
759760
}
760761

761-
for (const attribute of attributes) fieldString += ` ${attribute.field}: ${attribute.fieldArray?.entity}\n `
762+
for (const attribute of attributes) {
763+
const entity = attribute.fieldArray?.entity === 'enum' ? 'string[]' : attribute.fieldArray?.entity
764+
765+
fieldString += ` ${attribute.field}: ${entity}\n `
766+
}
762767

763768
const otherModelRelations = await fetchOtherModelRelations(model, modelName)
764769

@@ -950,7 +955,7 @@ async function generateModelString(
950955
951956
query = query.where(column, operator, value)
952957
953-
return await query.selectAll().execute()
958+
return await query.selectAll()
954959
}
955960
956961
async whereIs(criteria: Partial<${modelName}Type>, options: QueryOptions = {}) {
@@ -1140,6 +1145,8 @@ async function generateModelString(
11401145
11411146
const Model = ${modelName}Model
11421147
1148+
let queryInstance: any = null
1149+
11431150
// starting here, ORM functions
11441151
export async function find(id: number, fields?: (keyof ${modelName}Type)[]) {
11451152
let query = db.selectFrom('${tableName}').where('id', '=', id)
@@ -1194,7 +1201,14 @@ async function generateModelString(
11941201
return results.length
11951202
}
11961203
1197-
export async function get(criteria: Partial<${modelName}Type>, sort: { column: keyof ${modelName}Type, order: 'asc' | 'desc' } = { column: 'created_at', order: 'desc' }) {
1204+
export async function get() {
1205+
if (queryInstance)
1206+
return queryInstance.execute()
1207+
1208+
return await query.selectAll().execute()
1209+
}
1210+
1211+
export async function fetch(criteria: Partial<${modelName}Type>, sort: { column: keyof ${modelName}Type, order: 'asc' | 'desc' } = { column: 'created_at', order: 'desc' }) {
11981212
let query = db.selectFrom('${tableName}')
11991213
12001214
if (criteria.id)
@@ -1246,8 +1260,11 @@ async function generateModelString(
12461260
return await find(Number(result.insertId))
12471261
}
12481262
1249-
export async function first(): Promise<${modelName}Model> {
1250-
return await db.selectFrom('${tableName}')
1263+
export async function first(): Promise<AccessTokenModel> {
1264+
if (queryInstance)
1265+
return queryInstance.executeTakeFirst()
1266+
1267+
return await db.selectFrom('personal_access_tokens')
12511268
.selectAll()
12521269
.executeTakeFirst()
12531270
}
@@ -1294,11 +1311,13 @@ async function generateModelString(
12941311
throw new Error("Invalid number of arguments")
12951312
}
12961313
1297-
let query = db.selectFrom('${tableName}')
1314+
let query = db.selectFrom('personal_access_tokens')
12981315
12991316
query = query.where(column, operator, value)
13001317
1301-
return await query.selectAll().execute()
1318+
queryInstance = await query.selectAll()
1319+
1320+
return queryInstance
13021321
}
13031322
13041323
export async function whereIs(

storage/framework/core/orm/src/generated/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ProjectsTable } from '../../../../orm/src/models/Project'
22
import type { SubscriberEmailsTable } from '../../../../orm/src/models/SubscriberEmail'
3-
import type { AccessTokensTable } from '../../../../orm/src/models/AccessToken'
3+
import type { PersonalAccessTokensTable } from '../../../../orm/src/models/AccessToken'
44
import type { TeamsTable } from '../../../../orm/src/models/Team'
55
import type { SubscribersTable } from '../../../../orm/src/models/Subscriber'
66
import type { DeploymentsTable } from '../../../../orm/src/models/Deployment'
@@ -9,16 +9,16 @@ import type { UsersTable } from '../../../../orm/src/models/User'
99
import type { PostsTable } from '../../../../orm/src/models/Post'
1010
import type { Generated } from 'kysely'
1111

12-
export interface TeamAccessTokensTable {
12+
export interface TeamPersonalAccessTokensTable {
1313
id: Generated<number>
1414
team_id: number
1515
accesstoken_id: number
1616
}
1717
export interface Database {
1818
projects: ProjectsTable
1919
subscriber_emails: SubscriberEmailsTable
20-
access_tokens: AccessTokensTable
21-
team_access_tokens: TeamAccessTokensTable
20+
personal_access_tokens: PersonalAccessTokensTable
21+
team_personal_access_tokens: TeamPersonalAccessTokensTable
2222
teams: TeamsTable
2323
subscribers: SubscribersTable
2424
deployments: DeploymentsTable
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
export type ModelNames =
2-
| 'Project'
3-
| 'SubscriberEmail'
4-
| 'AccessToken'
5-
| 'Team'
6-
| 'Subscriber'
7-
| 'Deployment'
8-
| 'Release'
9-
| 'User'
10-
| 'Post'
1+
export type ModelNames = 'Project' | 'SubscriberEmail' | 'AccessToken' | 'Team' | 'Subscriber' | 'Deployment' | 'Release' | 'User' | 'Post'
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { Action } from '@stacksjs/actions'
2-
import type { ReleaseRequestType } from '../../types/requests'
32
import Release from '../src/models/Release'
3+
import type { ReleaseRequestType } from '../../types/requests'
44

55
export default new Action({
6-
name: 'Release Destroy',
7-
description: 'Release Destroy ORM Action',
8-
method: 'DELETE',
9-
async handle(request: ReleaseRequestType) {
10-
const id = request.getParam('id')
6+
name: 'Release Destroy',
7+
description: 'Release Destroy ORM Action',
8+
method: 'DELETE',
9+
async handle(request: ReleaseRequestType) {
10+
const id = request.getParam('id')
1111

12-
const model = await Release.findOrFail(Number(id))
12+
const model = await Release.findOrFail(Number(id))
1313

14-
model.delete()
14+
model.delete()
1515

16-
return 'Model deleted!'
17-
},
18-
})
16+
return 'Model deleted!'
17+
},
18+
})
19+
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Action } from '@stacksjs/actions'
2-
import type { ReleaseRequestType } from '../../types/requests'
32
import Release from '../src/models/Release'
3+
import type { ReleaseRequestType } from '../../types/requests'
44

55
export default new Action({
6-
name: 'Release Index',
7-
description: 'Release Index ORM Action',
8-
method: 'GET',
9-
async handle(request: ReleaseRequestType) {
10-
return await Release.all()
11-
},
12-
})
6+
name: 'Release Index',
7+
description: 'Release Index ORM Action',
8+
method: 'GET',
9+
async handle(request: ReleaseRequestType) {
10+
return await Release.all()
11+
},
12+
})
13+
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Action } from '@stacksjs/actions'
2-
import type { ReleaseRequestType } from '../../types/requests'
32
import Release from '../src/models/Release'
3+
import type { ReleaseRequestType } from '../../types/requests'
44

55
export default new Action({
6-
name: 'Release Show',
7-
description: 'Release Show ORM Action',
8-
method: 'GET',
9-
async handle(request: ReleaseRequestType) {
10-
const id = await request.getParam('id')
6+
name: 'Release Show',
7+
description: 'Release Show ORM Action',
8+
method: 'GET',
9+
async handle(request: ReleaseRequestType) {
10+
const id = await request.getParam('id')
1111

12-
return Release.findOrFail(Number(id))
13-
},
14-
})
12+
return Release.findOrFail(Number(id))
13+
},
14+
})
15+
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Action } from '@stacksjs/actions'
2-
import type { ReleaseRequestType } from '../../types/requests'
32
import Release from '../src/models/Release'
3+
import type { ReleaseRequestType } from '../../types/requests'
44

55
export default new Action({
6-
name: 'Release Store',
7-
description: 'Release Store ORM Action',
8-
method: 'POST',
9-
async handle(request: ReleaseRequestType) {
10-
await request.validate()
11-
const model = await Release.create(request.all())
6+
name: 'Release Store',
7+
description: 'Release Store ORM Action',
8+
method: 'POST',
9+
async handle(request: ReleaseRequestType) {
10+
await request.validate()
11+
const model = await Release.create(request.all())
1212

13-
return model
14-
},
15-
})
13+
return model
14+
},
15+
})
16+
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { Action } from '@stacksjs/actions'
2-
import type { ReleaseRequestType } from '../../types/requests'
32
import Release from '../src/models/Release'
3+
import type { ReleaseRequestType } from '../../types/requests'
44

55
export default new Action({
6-
name: 'Release Update',
7-
description: 'Release Update ORM Action',
8-
method: 'PATCH',
9-
async handle(request: ReleaseRequestType) {
10-
await request.validate()
6+
name: 'Release Update',
7+
description: 'Release Update ORM Action',
8+
method: 'PATCH',
9+
async handle(request: ReleaseRequestType) {
10+
await request.validate()
11+
12+
const id = request.getParam('id')
1113

12-
const id = request.getParam('id')
14+
const model = await Release.findOrFail(Number(id))
1315

14-
const model = await Release.findOrFail(Number(id))
15-
16-
return model.update(request.all())
17-
},
18-
})
16+
return model.update(request.all())
17+
},
18+
})
19+

0 commit comments

Comments
 (0)