chore: typescript version compatibilities and improvements

This commit is contained in:
James
2021-12-22 15:32:05 -05:00
parent 2e946a0aac
commit 30ec146298
12 changed files with 51 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
import { Response } from 'express';
import { Result } from '../login';
import { PayloadRequest } from '../../../express/types';
import { TypeWithID } from '../../../collections/config/types';
export type Options = {
collection: string
@@ -17,7 +18,7 @@ export type Options = {
showHiddenFields?: boolean
}
async function login(options: Options): Promise<Result> {
async function login<T extends TypeWithID = any>(options: Options): Promise<Result & { user: T}> {
const {
collection: collectionSlug,
req = {},

View File

@@ -8,6 +8,7 @@ import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
import { Field, fieldHasSubFields, fieldAffectsData } from '../../fields/config/types';
import { User } from '../types';
import { Collection } from '../../collections/config/types';
import { Payload } from '../..';
export type Result = {
user?: User,
@@ -28,7 +29,7 @@ export type Arguments = {
showHiddenFields?: boolean
}
async function login(incomingArgs: Arguments): Promise<Result> {
async function login(this: Payload, incomingArgs: Arguments): Promise<Result> {
const { config, operations, secret } = this;
let args = incomingArgs;
@@ -176,7 +177,7 @@ async function login(incomingArgs: Arguments): Promise<Result> {
id: user.id,
data: user,
hook: 'afterRead',
operation: 'login',
operation: 'read',
overrideAccess,
flattenLocales: true,
showHiddenFields,

View File

@@ -1,10 +1,16 @@
import { Document } from '../../types';
import { Forbidden } from '../../errors';
import { Payload } from '../..';
import { Collection } from '../../collections/config/types';
import { PayloadRequest } from '../../express/types';
import { Collection, TypeWithID } from '../../collections/config/types';
export type Arguments = {
collection: Collection
data: {
email: string
password: string
}
req: PayloadRequest
}
export type Result = {
@@ -23,6 +29,11 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
},
},
},
req: {
payload,
},
req,
data,
} = args;
const count = await Model.countDocuments({});
@@ -33,14 +44,16 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
// Register first user
// /////////////////////////////////////
let result = await this.operations.collections.create({
...args,
const result = await payload.create<TypeWithID>({
req,
collection: slug,
data,
overrideAccess: true,
});
// auto-verify (if applicable)
if (verify) {
await this.update({
await payload.update({
id: result.id,
collection: slug,
data: {
@@ -53,18 +66,19 @@ async function registerFirstUser(this: Payload, args: Arguments): Promise<Result
// Log in new user
// /////////////////////////////////////
const { token } = await this.operations.collections.auth.login({
const { token } = await payload.login({
...args,
collection: slug,
});
result = {
const resultToReturn = {
...result,
token,
};
return {
message: 'Registered and logged in successfully. Welcome!',
user: result,
user: resultToReturn,
};
}