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,
};
}

View File

@@ -7,6 +7,7 @@ import { Forbidden, NotFound } from '../../errors';
import executeAccess from '../../auth/executeAccess';
import { Where } from '../../types';
import { hasWhereAccessResult } from '../../auth/types';
import { Payload } from '../..';
export type Arguments = {
collection: Collection
@@ -19,7 +20,7 @@ export type Arguments = {
depth?: number
}
async function findByID<T extends TypeWithID = any>(incomingArgs: Arguments): Promise<T> {
async function findByID<T extends TypeWithID = any>(this: Payload, incomingArgs: Arguments): Promise<T> {
let args = incomingArgs;
// /////////////////////////////////////
@@ -48,7 +49,7 @@ async function findByID<T extends TypeWithID = any>(incomingArgs: Arguments): Pr
},
disableErrors,
currentDepth,
overrideAccess,
overrideAccess = false,
showHiddenFields,
} = args;

View File

@@ -2,7 +2,7 @@ import { Where } from '../../types';
import { PayloadRequest } from '../../express/types';
import executeAccess from '../../auth/executeAccess';
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
import { Collection } from '../config/types';
import { Collection, CollectionModel } from '../config/types';
import { hasWhereAccessResult } from '../../auth/types';
import flattenWhereConstraints from '../../utilities/flattenWhereConstraints';
import { buildSortParam } from '../../mongoose/buildSortParam';
@@ -39,7 +39,7 @@ async function findRevisions<T extends TypeWithRevision<T> = any>(this: Payload,
showHiddenFields,
} = args;
const RevisionsModel = this.revisions[collectionConfig.slug];
const RevisionsModel = this.revisions[collectionConfig.slug] as CollectionModel;
// /////////////////////////////////////
// Access

View File

@@ -1,3 +1,4 @@
import { PayloadRequest } from '../../../express/types';
import { Document } from '../../../types';
import getFileByPath from '../../../uploads/getFileByPath';
@@ -13,6 +14,7 @@ export type Options = {
showHiddenFields?: boolean
filePath?: string
overwriteExistingFiles?: boolean
req: PayloadRequest
}
export default async function create(options: Options): Promise<Document> {
const {
@@ -27,6 +29,7 @@ export default async function create(options: Options): Promise<Document> {
showHiddenFields,
filePath,
overwriteExistingFiles = false,
req,
} = options;
const collection = this.collections[collectionSlug];
@@ -40,6 +43,7 @@ export default async function create(options: Options): Promise<Document> {
showHiddenFields,
overwriteExistingFiles,
req: {
...req,
user,
payloadAPI: 'local',
locale,

View File

@@ -6,6 +6,7 @@ export type Options = {
collection: string
id: string
depth?: number
currentDepth?: number
locale?: string
fallbackLocale?: string
user?: Document
@@ -19,6 +20,7 @@ export default async function findByID<T extends TypeWithID = any>(options: Opti
const {
collection: collectionSlug,
depth,
currentDepth,
id,
locale = this?.config?.localization?.defaultLocale,
fallbackLocale = null,
@@ -33,6 +35,7 @@ export default async function findByID<T extends TypeWithID = any>(options: Opti
return this.operations.collections.findByID({
depth,
currentDepth,
id,
collection,
overrideAccess,

View File

@@ -4,7 +4,7 @@ import getFileByPath from '../../../uploads/getFileByPath';
export type Options = {
collection: string
id: string
id: string | number
data: Record<string, unknown>
depth?: number
locale?: string

View File

@@ -23,7 +23,7 @@ import { saveCollectionRevision } from '../../revisions/saveCollectionRevision';
export type Arguments = {
collection: Collection
req: PayloadRequest
id: string
id: string | number
data: Record<string, unknown>
depth?: number
disableVerificationEmail?: boolean

View File

@@ -40,14 +40,14 @@ const populate = async ({
let populatedRelationship;
if (depth && currentDepth <= depth) {
populatedRelationship = await payload.operations.collections.findByID({
populatedRelationship = await payload.findByID({
req,
collection: relatedCollection,
id: idString,
collection: relatedCollection.config.slug,
id: idString as string,
depth,
currentDepth: currentDepth + 1,
overrideAccess,
disableErrors: true,
depth,
});
}

View File

@@ -39,12 +39,11 @@ const populate = async ({
}) => {
const dataRef = data as Record<string, unknown>;
const doc = await payload.operations.collections.findByID({
req: {
...req,
payloadAPI: 'local',
},
collection,
const newReq = { ...req, payloadAPI: 'local' } as PayloadRequest;
const doc = await payload.findByID({
req: newReq,
collection: collection.config.slug,
id,
currentDepth: currentDepth + 1,
overrideAccess,

View File

@@ -49,6 +49,7 @@ import { Options as DeleteOptions } from './collections/operations/local/delete'
import { Options as FindRevisionsOptions } from './collections/operations/local/findRevisions';
import { Options as FindRevisionByIDOptions } from './collections/operations/local/findRevisionByID';
import { Options as RestoreRevisionOptions } from './collections/operations/local/restoreRevision';
import { Result } from './auth/operations/login';
require('isomorphic-fetch');
@@ -314,7 +315,7 @@ export class Payload {
// graphql operations & request handlers, where
// tests
login = async (options): Promise<any> => {
login = async <T extends TypeWithID = any>(options): Promise<Result & { user: T}> => {
let { login } = localOperations.auth;
login = login.bind(this);
return login(options);