Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Add types declaration file, install typescript as devDependency #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
61 changes: 61 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Model, Page, TransactionOrKnex } from 'objection-2';
import { AccessControl as AC3 } from 'role-acl-3';
import { AccessControl as AC4 } from 'role-acl-4';

declare module 'objection-authorize' {
class AuthQueryBuilder<M extends Model, R = M[]> {
ArrayQueryBuilderType: AuthQueryBuilder<M, M[]>;
SingleQueryBuilderType: AuthQueryBuilder<M, M>;
NumberQueryBuilderType: AuthQueryBuilder<M, number>;
PageQueryBuilderType: AuthQueryBuilder<M, Page<M>>;

insert<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

insertAndFetch<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

patch<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

patchAndFetch<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

patchAndFetchById<T>(id: number, body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

update<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

updateAndFetch<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

updateAndFetchById<T>(id: number, body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

delete<T>(body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

deleteById<T>(id: number, body: T): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

action(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

first<T>(): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

authorize<T extends typeof Model>(user: T, resource: object, optOverride: object): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
}

interface AuthInstance<T extends typeof Model> {
QueryBuilderType: AuthQueryBuilder<this & T['prototype']>;

$query(trx: TransactionOrKnex): AuthQueryBuilder<Model>['SingleQueryBuilderType'] & this['QueryBuilderType']['SingleQueryBuilderType'];
$relatedQuery(relation: string, trx: TransactionOrKnex): AuthQueryBuilder<Model>['SingleQueryBuilderType'] & this['QueryBuilderType']['SingleQueryBuilderType'];
}

interface AuthStatic<T extends typeof Model> {
QueryBuilder: typeof AuthQueryBuilder;

new(): AuthInstance<T> & T['prototype'];
}

export default function authorize(acl: typeof AC3 | typeof AC4, library: string, opts?: {
defaultRole?: string
unauthenticatedErrorCode?: number
unauthorizedErrorCode?: number
userFromResult?: boolean
contextKey?: string
roleFromUser?: (user: Model) => string
resourceAugments?: { true: boolean; false: boolean; undefined: undefined }
}): <T extends typeof Model>(model: T) => AuthStatic<T> & Omit<T, 'new'> & T['prototype'];
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"prettier-standard": "^16.1.0",
"role-acl-3": "npm:role-acl@<4.0.0",
"role-acl-4": "npm:role-acl@>=4.3.2",
"sqlite3": "^4.1.1"
"sqlite3": "^4.1.1",
"typescript": "^3.8.3"
},
"peerDependencies": {
"objection": "^1 || ^2"
Expand Down