Skip to content

Commit

Permalink
Merge branch 'beta' into fix/versions-permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed Apr 26, 2024
2 parents 266021f + 3a43fd3 commit 24deadb
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 88 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/exports/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { addDataAndFileToRequest } from '../utilities/addDataAndFileToRequest.js'
export { addLocalesToRequestFromData, sanitizeLocales } from '../utilities/addLocalesToRequest.js'
export { traverseFields } from '../utilities/buildFieldSchemaMap/traverseFields.js'
export { createPayloadRequest as createBasePayloadRequest } from '../utilities/createPayloadRequest.js'
export { createPayloadRequest } from '../utilities/createPayloadRequest.js'
export { getNextRequestI18n } from '../utilities/getNextRequestI18n.js'
export { getPayloadHMR, reload } from '../utilities/getPayloadHMR.js'
export { headersWithCors } from '../utilities/headersWithCors.js'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FileShape, NextFileUploadOptions } from './index.js'
import type { FetchAPIFileUploadOptions, FileShape } from './index.js'

import {
checkAndMakeDir,
Expand All @@ -12,7 +12,7 @@ import {
type MoveFile = (
filePath: string,
options: FileFactoryOptions,
fileUploadOptions: NextFileUploadOptions,
fileUploadOptions: FetchAPIFileUploadOptions,
) => (resolve: () => void, reject: () => void) => void

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ type FileFactoryOptions = {
}
type FileFactory = (
options: FileFactoryOptions,
fileUploadOptions: NextFileUploadOptions,
fileUploadOptions: FetchAPIFileUploadOptions,
) => FileShape
export const fileFactory: FileFactory = (options, fileUploadOptions) => {
// see: https://github.com/richardgirges/express-fileupload/issues/14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import crypto from 'crypto'
import fs, { WriteStream } from 'fs'
import path from 'path'

import type { NextFileUploadOptions } from './index.js'
import type { FetchAPIFileUploadOptions } from './index.js'

import { checkAndMakeDir, debugLog, deleteFile, getTempFilename } from './utilities.js'

type Handler = (
options: NextFileUploadOptions,
options: FetchAPIFileUploadOptions,
fieldname: string,
filename: string,
) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type FileShape = {
truncated: boolean
}

export type NextFileUploadOptions = {
export type FetchAPIFileUploadOptions = {
/**
* Returns a HTTP 413 when the file is bigger than the size limit if `true`.
* Otherwise, it will add a `truncated = true` to the resulting file structure.
Expand Down Expand Up @@ -132,28 +132,28 @@ export type NextFileUploadOptions = {
useTempFiles?: boolean | undefined
} & Partial<BusboyConfig>

type NextFileUploadResponseFile = {
type FetchAPIFileUploadResponseFile = {
data: Buffer
mimetype: string
name: string
size: number
tempFilePath?: string
}

export type NextFileUploadResponse = {
export type FetchAPIFileUploadResponse = {
error?: {
code: number
message: string
}
fields: Record<string, string>
files: Record<string, NextFileUploadResponseFile>
files: Record<string, FetchAPIFileUploadResponseFile>
}

type NextFileUpload = (args: {
options?: NextFileUploadOptions
type FetchAPIFileUpload = (args: {
options?: FetchAPIFileUploadOptions
request: Request
}) => Promise<NextFileUploadResponse>
export const nextFileUpload: NextFileUpload = async ({ options, request }) => {
}) => Promise<FetchAPIFileUploadResponse>
export const fetchAPIFileUpload: FetchAPIFileUpload = async ({ options, request }) => {
const uploadOptions = { ...DEFAULT_OPTIONS, ...options }
if (!isEligibleRequest(request)) {
debugLog(uploadOptions, 'Request is not eligible for file upload!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Busboy from 'busboy'
import httpStatus from 'http-status'
import { APIError } from 'payload/errors'

import type { NextFileUploadOptions, NextFileUploadResponse } from './index.js'
import type { FetchAPIFileUploadOptions, FetchAPIFileUploadResponse } from './index.js'

import { fileFactory } from './fileFactory.js'
import { memHandler, tempFileHandler } from './handlers.js'
Expand All @@ -13,9 +13,9 @@ import { buildFields, debugLog, isFunc, parseFileName } from './utilities.js'
const waitFlushProperty = Symbol('wait flush property symbol')

type ProcessMultipart = (args: {
options: NextFileUploadOptions
options: FetchAPIFileUploadOptions
request: Request
}) => Promise<NextFileUploadResponse>
}) => Promise<FetchAPIFileUploadResponse>
export const processMultipart: ProcessMultipart = async ({ options, request }) => {
let parsingRequest = true

Expand All @@ -29,7 +29,7 @@ export const processMultipart: ProcessMultipart = async ({ options, request }) =
failedResolvingFiles = rej
})

const result: NextFileUploadResponse = {
const result: FetchAPIFileUploadResponse = {
fields: undefined,
files: undefined,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export const processNested = function (data) {
keys = Object.keys(data)

for (let i = 0; i < keys.length; i++) {
let key = keys[i],
const key = keys[i],
value = data[key],
current = d,
keyParts = key.replace(new RegExp(/\[/g), '.').replace(new RegExp(/\]/g), '').split('.')
let current = d

for (let index = 0; index < keyParts.length; index++) {
const k = keyParts[index]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import { Readable } from 'stream'

import type { NextFileUploadOptions } from './index.js'
import type { FetchAPIFileUploadOptions } from './index.js'

// Parameters for safe file name parsing.
const SAFE_FILE_NAME_REGEX = /[^\w-]/g
Expand All @@ -16,7 +16,7 @@ let tempCounter = 0
/**
* Logs message to console if options.debug option set to true.
*/
export const debugLog = (options: NextFileUploadOptions, msg: string) => {
export const debugLog = (options: FetchAPIFileUploadOptions, msg: string) => {
const opts = options || {}
if (!opts.debug) return false
console.log(`Next-file-upload: ${msg}`) // eslint-disable-line
Expand Down Expand Up @@ -108,7 +108,7 @@ export const buildFields: BuildFields = (instance, field, value) => {
* Creates a folder if it does not exist
* for file specified in the path variable
*/
type CheckAndMakeDir = (fileUploadOptions: NextFileUploadOptions, filePath: string) => boolean
type CheckAndMakeDir = (fileUploadOptions: FetchAPIFileUploadOptions, filePath: string) => boolean
export const checkAndMakeDir: CheckAndMakeDir = (fileUploadOptions, filePath) => {
if (!fileUploadOptions.createParentPath) return false
// Check whether folder for the file exists.
Expand Down Expand Up @@ -271,7 +271,7 @@ export const parseFileNameExtension: ParseFileNameExtension = (preserveExtension
/**
* Parse file name and extension.
*/
type ParseFileName = (opts: NextFileUploadOptions, fileName: string) => string
type ParseFileName = (opts: FetchAPIFileUploadOptions, fileName: string) => string
export const parseFileName: ParseFileName = (opts, fileName) => {
// Check fileName argument
if (!fileName || typeof fileName !== 'string') return getTempFilename()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/next/src/routes/rest/files/getFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import httpStatus from 'http-status'
import path from 'path'
import { APIError } from 'payload/errors'

import { streamFile } from '../../../next-stream-file/index.js'
import { streamFile } from '../../../fetchAPI-stream-file/index.js'
import { headersWithCors } from '../../../utilities/headersWithCors.js'
import { routeError } from '../routeError.js'
import { checkFileAccess } from './checkFileAccess.js'
Expand Down
25 changes: 13 additions & 12 deletions packages/next/src/routes/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ const handleCustomEndpoints = ({
})

if (customEndpoint) {
payloadRequest.routeParams = handlerParams
payloadRequest.routeParams = {
...payloadRequest.routeParams,
...handlerParams,
}
return customEndpoint.handler(payloadRequest)
}
}
Expand Down Expand Up @@ -211,9 +214,6 @@ export const GET =
try {
req = await createPayloadRequest({
config,
params: {
collection: slug1,
},
request,
})

Expand All @@ -227,6 +227,7 @@ export const GET =
collection = req.payload.collections?.[slug1]

if (collection) {
req.routeParams.collection = slug1
const disableEndpoints = endpointsAreDisabled({
endpoints: collection.config.endpoints,
request,
Expand Down Expand Up @@ -295,12 +296,12 @@ export const GET =
}
} else if (slug1 === 'globals') {
const globalConfig = req.payload.config.globals.find((global) => global.slug === slug2)
req.routeParams.global = globalConfig.slug

const disableEndpoints = endpointsAreDisabled({
endpoints: globalConfig.endpoints,
request,
})

if (disableEndpoints) return disableEndpoints

const customEndpointResponse = await handleCustomEndpoints({
Expand Down Expand Up @@ -391,7 +392,6 @@ export const POST =
try {
req = await createPayloadRequest({
config,
params: { collection: slug1 },
request,
})

Expand All @@ -405,6 +405,7 @@ export const POST =
if (disableEndpoints) return disableEndpoints

if (collection) {
req.routeParams.collection = slug1
const disableEndpoints = endpointsAreDisabled({
endpoints: collection.config.endpoints,
request,
Expand Down Expand Up @@ -466,6 +467,8 @@ export const POST =
}
} else if (slug1 === 'globals' && slug2) {
const globalConfig = req.payload.config.globals.find((global) => global.slug === slug2)
req.routeParams.global = globalConfig.slug

const disableEndpoints = endpointsAreDisabled({
endpoints: globalConfig.endpoints,
request,
Expand Down Expand Up @@ -553,9 +556,6 @@ export const DELETE =
try {
req = await createPayloadRequest({
config,
params: {
collection: slug1,
},
request,
})
collection = req.payload.collections?.[slug1]
Expand All @@ -567,6 +567,8 @@ export const DELETE =
if (disableEndpoints) return disableEndpoints

if (collection) {
req.routeParams.collection = slug1

const disableEndpoints = endpointsAreDisabled({
endpoints: collection.config.endpoints,
request,
Expand Down Expand Up @@ -635,9 +637,6 @@ export const PATCH =
try {
req = await createPayloadRequest({
config,
params: {
collection: slug1,
},
request,
})
collection = req.payload.collections?.[slug1]
Expand All @@ -649,6 +648,8 @@ export const PATCH =
if (disableEndpoints) return disableEndpoints

if (collection) {
req.routeParams.collection = slug1

const disableEndpoints = endpointsAreDisabled({
endpoints: collection.config.endpoints,
request,
Expand Down

0 comments on commit 24deadb

Please sign in to comment.