Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 7ca62fe

Browse files
authoredJan 31, 2023
feat: return displayName with json config from listFunction, listFunctions and listFunctionsFiles (#1329)
1 parent 166ea95 commit 7ca62fe

File tree

8 files changed

+93
-10
lines changed

8 files changed

+93
-10
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ Each object has the following properties:
315315
Function's name. This is the one used in the Function URL. For example, if a Function is a `myFunc.js` regular file,
316316
the `name` is `myFunc` and the URL is `https://{hostname}/.netlify/functions/myFunc`.
317317

318+
- `displayName` `string`
319+
320+
If there was a user-defined configuration object applied to the function, and it had a `name` defined. This will be returned here.
321+
318322
- `mainFile`: `string`
319323

320324
Absolute path to the Function's main file. If the Function is a Node.js directory, this is its `index.js` or

‎src/main.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface ListedFunction {
2121
runtime: RuntimeType
2222
extension: string
2323
schedule?: string
24+
displayName?: string
2425
}
2526

2627
type ListedFunctionFile = ListedFunction & {
@@ -30,6 +31,7 @@ type ListedFunctionFile = ListedFunction & {
3031
interface ListFunctionsOptions {
3132
basePath?: string
3233
config?: Config
34+
configFileDirectories?: string[]
3335
featureFlags?: FeatureFlags
3436
parseISC?: boolean
3537
}
@@ -39,7 +41,8 @@ interface AugmentedFunctionSource extends FunctionSource {
3941
}
4042

4143
const augmentWithISC = async (func: FunctionSource): Promise<AugmentedFunctionSource> => {
42-
// ISC is currently only supported in JavaScript and TypeScript functions.
44+
// ISC is currently only supported in JavaScript and TypeScript functions
45+
// and only supports scheduled functions.
4346
if (func.runtime.name !== RuntimeType.JAVASCRIPT) {
4447
return func
4548
}
@@ -55,14 +58,15 @@ export const listFunctions = async function (
5558
{
5659
featureFlags: inputFeatureFlags,
5760
config,
61+
configFileDirectories,
5862
parseISC = false,
59-
}: { featureFlags?: FeatureFlags; config?: Config; parseISC?: boolean } = {},
63+
}: { featureFlags?: FeatureFlags; config?: Config; configFileDirectories?: string[]; parseISC?: boolean } = {},
6064
) {
6165
const featureFlags = getFlags(inputFeatureFlags)
6266
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
6367
const paths = await listFunctionsDirectories(srcFolders)
6468
const cache = new RuntimeCache()
65-
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, featureFlags })
69+
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, configFileDirectories, featureFlags })
6670
const functions = [...functionsMap.values()]
6771
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
6872

@@ -75,12 +79,13 @@ export const listFunction = async function (
7579
{
7680
featureFlags: inputFeatureFlags,
7781
config,
82+
configFileDirectories,
7883
parseISC = false,
79-
}: { featureFlags?: FeatureFlags; config?: Config; parseISC?: boolean } = {},
84+
}: { featureFlags?: FeatureFlags; config?: Config; configFileDirectories?: string[]; parseISC?: boolean } = {},
8085
) {
8186
const featureFlags = getFlags(inputFeatureFlags)
8287
const cache = new RuntimeCache()
83-
const func = await getFunctionFromPath(path, { cache, config, featureFlags })
88+
const func = await getFunctionFromPath(path, { cache, config, configFileDirectories, featureFlags })
8489

8590
if (!func) {
8691
return
@@ -94,13 +99,19 @@ export const listFunction = async function (
9499
// List all Netlify Functions files for a specific directory
95100
export const listFunctionsFiles = async function (
96101
relativeSrcFolders: string | string[],
97-
{ basePath, config, featureFlags: inputFeatureFlags, parseISC = false }: ListFunctionsOptions = {},
102+
{
103+
basePath,
104+
config,
105+
configFileDirectories,
106+
featureFlags: inputFeatureFlags,
107+
parseISC = false,
108+
}: ListFunctionsOptions = {},
98109
) {
99110
const featureFlags = getFlags(inputFeatureFlags)
100111
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
101112
const paths = await listFunctionsDirectories(srcFolders)
102113
const cache = new RuntimeCache()
103-
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, featureFlags })
114+
const functionsMap = await getFunctionsFromPaths(paths, { cache, config, configFileDirectories, featureFlags })
104115
const functions = [...functionsMap.values()]
105116
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
106117
const listedFunctionsFiles = await Promise.all(
@@ -118,7 +129,14 @@ const getListedFunction = function ({
118129
config,
119130
inSourceConfig,
120131
}: AugmentedFunctionSource): ListedFunction {
121-
return { name, mainFile, runtime: runtime.name, extension, schedule: inSourceConfig?.schedule ?? config.schedule }
132+
return {
133+
name,
134+
displayName: config.name,
135+
mainFile,
136+
runtime: runtime.name,
137+
extension,
138+
schedule: inSourceConfig?.schedule ?? config.schedule,
139+
}
122140
}
123141

124142
const getListedFunctionFiles = async function (

‎src/runtimes/index.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,23 @@ export const getFunctionsFromPaths = async (
121121
*/
122122
export const getFunctionFromPath = async (
123123
path: string,
124-
{ cache, config, featureFlags = defaultFlags }: { cache: RuntimeCache; config?: Config; featureFlags?: FeatureFlags },
124+
{
125+
cache,
126+
config,
127+
configFileDirectories,
128+
featureFlags = defaultFlags,
129+
}: { cache: RuntimeCache; config?: Config; configFileDirectories?: string[]; featureFlags?: FeatureFlags },
125130
): Promise<FunctionSource | undefined> => {
126131
for (const runtime of RUNTIMES) {
127132
const func = await runtime.findFunctionInPath({ path, cache, featureFlags })
128133

129134
if (func) {
130-
const functionConfig = await getConfigForFunction({ config, func: { ...func, runtime }, featureFlags })
135+
const functionConfig = await getConfigForFunction({
136+
config,
137+
configFileDirectories,
138+
func: { ...func, runtime },
139+
featureFlags,
140+
})
131141

132142
return {
133143
...func,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const handler = async () => ({
2+
body: `Hello world! A whole new one!`,
3+
})
4+
5+
export { handler };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"config": { "name": "A Display Name" },
3+
"version": 1
4+
}

‎tests/list_function.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,22 @@ describe('listFunction', () => {
2020
schedule: '@daily',
2121
})
2222
})
23+
24+
test('listFunction includes json configured functions with a name and returns it as a displayName', async () => {
25+
const dir = join(FIXTURES_DIR, 'json-config/.netlify/functions-internal/')
26+
const mainFile = join(dir, 'simple.js')
27+
const func = await listFunction(mainFile, {
28+
configFileDirectories: [dir],
29+
featureFlags: {
30+
project_deploy_configuration_api_use_per_function_configuration_files: true,
31+
},
32+
})
33+
expect(func).toEqual({
34+
displayName: 'A Display Name',
35+
extension: '.js',
36+
mainFile,
37+
name: 'simple',
38+
runtime: 'js',
39+
})
40+
})
2341
})

‎tests/list_functions.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ describe('listFunctions', () => {
8686
expect(func.schedule).toBe('@daily')
8787
})
8888
})
89+
90+
test('listFunctions includes json configured functions with a name and returns it as a displayName', async () => {
91+
const dir = join(FIXTURES_DIR, 'json-config/.netlify/functions-internal/')
92+
const [func] = await listFunctions([dir], {
93+
configFileDirectories: [dir],
94+
featureFlags: {
95+
project_deploy_configuration_api_use_per_function_configuration_files: true,
96+
},
97+
})
98+
99+
expect(func.displayName).toBe('A Display Name')
100+
})
89101
})

‎tests/list_functions_files.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,16 @@ describe('listFunctionsFiles', () => {
251251
expect(warn).toHaveBeenCalledWith(expect.stringContaining('Darwin/Arm64'))
252252
warn.mockRestore()
253253
})
254+
255+
test('listFunctionsFiles includes json configured functions with a name and returns it as a displayName', async () => {
256+
const dir = join(FIXTURES_DIR, 'json-config/.netlify/functions-internal/')
257+
const [func] = await listFunctionsFiles([dir], {
258+
configFileDirectories: [dir],
259+
featureFlags: {
260+
project_deploy_configuration_api_use_per_function_configuration_files: true,
261+
},
262+
})
263+
264+
expect(func.displayName).toBe('A Display Name')
265+
})
254266
})

1 commit comments

Comments
 (1)

github-actions[bot] commented on Jan 31, 2023

@github-actions[bot]
Contributor

⏱ Benchmark results

  • largeDepsEsbuild: 2.1s
  • largeDepsNft: 7.7s
  • largeDepsZisi: 15.1s
This repository has been archived.