Skip to content

Commit 7925754

Browse files
chore: convert functions helper to TS (#875)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent eed17a2 commit 7925754

File tree

4 files changed

+40
-148
lines changed

4 files changed

+40
-148
lines changed

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const HIDDEN_PATHS = [
1717

1818
export const ODB_FUNCTION_PATH = `/.netlify/builders/${ODB_FUNCTION_NAME}`
1919
export const HANDLER_FUNCTION_PATH = `/.netlify/functions/${HANDLER_FUNCTION_NAME}`
20-
20+
export const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
2121
export const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
2222
export const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
2323
export const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g

src/helpers/config.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import { readJSON } from 'fs-extra'
2+
import type { NextConfigComplete } from 'next/dist/server/config-shared'
23
import { join, dirname, relative } from 'pathe'
34
import slash from 'slash'
45

56
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants'
67

7-
import { RequiredServerFiles } from './requiredServerFilesType'
8+
export interface RequiredServerFiles {
9+
version?: number
10+
config?: NextConfigComplete
11+
appDir?: string
12+
files?: string[]
13+
ignore?: string[]
14+
}
15+
16+
export type NextConfig = Pick<RequiredServerFiles, 'appDir' | 'ignore'> & NextConfigComplete
817

918
const defaultFailBuild = (message: string, { error }): never => {
1019
throw new Error(`${message}\n${error && error.stack}`)
1120
}
1221

13-
export const getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild }) {
22+
export const getNextConfig = async function getNextConfig({
23+
publish,
24+
failBuild = defaultFailBuild,
25+
}): Promise<NextConfig> {
1426
try {
1527
const { config, appDir, ignore }: RequiredServerFiles = await readJSON(join(publish, 'required-server-files.json'))
1628
if (!config) {

src/helpers/functions.js renamed to src/helpers/functions.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const { copyFile, ensureDir, writeFile, writeJSON } = require('fs-extra')
2-
const { join, relative } = require('pathe')
1+
import { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
2+
import { copyFile, ensureDir, writeFile, writeJSON } from 'fs-extra'
3+
import type { ImageConfigComplete } from 'next/dist/server/image-config'
4+
import { join, relative } from 'pathe'
35

4-
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } = require('../constants')
5-
const getHandler = require('../templates/getHandler')
6-
const { getPageResolver } = require('../templates/getPageResolver')
6+
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
7+
import getHandler from '../templates/getHandler'
8+
import { getPageResolver } from '../templates/getPageResolver'
79

8-
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
9-
10-
exports.generateFunctions = async (
11-
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR },
12-
appDir,
13-
) => {
10+
export const generateFunctions = async (
11+
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
12+
appDir: string,
13+
): Promise<void> => {
1414
const functionsDir = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
1515
const bridgeFile = require.resolve('@vercel/node/dist/bridge')
1616

1717
const functionDir = join(process.cwd(), functionsDir, HANDLER_FUNCTION_NAME)
1818
const publishDir = relative(functionDir, join(process.cwd(), PUBLISH_DIR))
1919

20-
const writeHandler = async (func, isODB) => {
20+
const writeHandler = async (func: string, isODB: boolean) => {
2121
const handlerSource = await getHandler({ isODB, publishDir, appDir: relative(functionDir, appDir) })
2222
await ensureDir(join(functionsDir, func))
2323
await writeFile(join(functionsDir, func, `${func}.js`), handlerSource)
@@ -36,11 +36,15 @@ exports.generateFunctions = async (
3636
* Writes a file in each function directory that contains references to every page entrypoint.
3737
* This is just so that the nft bundler knows about them. We'll eventually do this better.
3838
*/
39-
exports.generatePagesResolver = async ({
39+
export const generatePagesResolver = async ({
4040
constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
4141
netlifyConfig,
4242
target,
43-
}) => {
43+
}: {
44+
constants: NetlifyPluginConstants
45+
netlifyConfig: NetlifyConfig
46+
target: string
47+
}): Promise<void> => {
4448
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
4549

4650
const jsSource = await getPageResolver({
@@ -53,12 +57,17 @@ exports.generatePagesResolver = async ({
5357
}
5458

5559
// Move our next/image function into the correct functions directory
56-
exports.setupImageFunction = async ({
60+
export const setupImageFunction = async ({
5761
constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
5862
imageconfig = {},
5963
netlifyConfig,
6064
basePath,
61-
}) => {
65+
}: {
66+
constants: NetlifyPluginConstants
67+
netlifyConfig: NetlifyConfig
68+
basePath: string
69+
imageconfig: Partial<ImageConfigComplete>
70+
}): Promise<void> => {
6271
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC
6372
const functionName = `${IMAGE_FUNCTION_NAME}.js`
6473
const functionDirectory = join(functionsPath, IMAGE_FUNCTION_NAME)

src/helpers/requiredServerFilesType.ts

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)