Repository: netlify/build
Pull request #3851 (2021-11-16 13:12:00)

fix: add types for `utils.functions`
#### Summary

Adds missing types for `utils.functions.*`. 

**A picture of a cute animal (not mandatory, but encouraged)**



Patch:
diff --git a/packages/build/types/index.d.ts b/packages/build/types/index.d.ts
index d173311c3..9488400ba 100644
--- a/packages/build/types/index.d.ts
+++ b/packages/build/types/index.d.ts
@@ -4,3 +4,4 @@ export { OnBuild, OnEnd, OnError, OnPostBuild, OnPreBuild, OnSuccess } from './n
 export { NetlifyConfig } from './config/netlify_config'
 export { NetlifyPluginUtils } from './options'
 export { NetlifyPluginConstants } from './netlify_plugin_constants'
+export { ListedFunction, ListedFunctionFile } from './options/netlify_plugin_functions_util'
diff --git a/packages/build/types/options/netlify_plugin_functions_util.d.ts b/packages/build/types/options/netlify_plugin_functions_util.d.ts
new file mode 100644
index 000000000..a4cde0887
--- /dev/null
+++ b/packages/build/types/options/netlify_plugin_functions_util.d.ts
@@ -0,0 +1,32 @@
+import type { listFunctions, listFunctionsFiles } from '@netlify/zip-it-and-ship-it'
+
+type Resolved<T> = T extends Promise<infer U> ? U : T
+
+export type ListedFunction = Resolved<ReturnType<typeof listFunctions>>[0]
+export type ListedFunctionFile = Resolved<ReturnType<typeof listFunctionsFiles>>[0]
+
+export interface NetlifyPluginFunctionsUtil {
+  /**
+   * Returns the list of Netlify Functions main files as a Promise resolving to an array of objects with the following properties:
+   *
+   * - `name`: Function name, as used in the URL `https://{hostname}/.netlify/functions/{name}`
+   * - `mainFile`: absolute path to the Function's main file
+   * - `extension`: file extension of the Function's main file. For Go Functions, this might be an empty string.  For Node.js Functions, this is either `.js` or `.zip`.
+   * - `runtime` `"js" | "go"`: Function's language runtime. TypeScript Functions use the "js" runtime
+   */
+  list(): Promise<Array<ListedFunction>>
+
+  /**
+   * Same as `list()` except it also returns the files required by the Functions' main files. This is much slower. The object have the following additional member:
+   *
+   * - `srcFile`: absolute path to the file
+   */
+  listAll(): Promise<Array<ListedFunctionFile>>
+
+  /**
+   * Add a Functions file or directory to a build.
+   *
+   * @param path Path to the function file or directory.
+   */
+  add(path: string): Promise<void>
+}
diff --git a/packages/build/types/options/netlify_plugin_utils.d.ts b/packages/build/types/options/netlify_plugin_utils.d.ts
index f656afa2c..3a320680c 100644
--- a/packages/build/types/options/netlify_plugin_utils.d.ts
+++ b/packages/build/types/options/netlify_plugin_utils.d.ts
@@ -1,5 +1,6 @@
 import { NetlifyPluginBuildUtil } from './netlify_plugin_build_util'
 import { NetlifyPluginCacheUtil } from './netlify_plugin_cache_util'
+import { NetlifyPluginFunctionsUtil } from './netlify_plugin_functions_util'
 import { NetlifyPluginGitUtil } from './netlify_plugin_git_util'
 import { NetlifyPluginRunUtil } from './netlify_plugin_run_util'
 import { NetlifyPluginStatusUtil } from './netlify_plugin_status_util'
@@ -10,4 +11,5 @@ export interface NetlifyPluginUtils {
   cache: NetlifyPluginCacheUtil
   run: NetlifyPluginRunUtil
   git: NetlifyPluginGitUtil
+  functions: NetlifyPluginFunctionsUtil
 }


Test patch:
diff --git a/packages/build/test-d/options/netlify_plugin_functions_util.ts b/packages/build/test-d/options/netlify_plugin_functions_util.ts
new file mode 100644
index 000000000..2085bb376
--- /dev/null
+++ b/packages/build/test-d/options/netlify_plugin_functions_util.ts
@@ -0,0 +1,32 @@
+import { OnPreBuild, NetlifyPluginUtils, ListedFunction, ListedFunctionFile } from '@netlify/build'
+import { expectType } from 'tsd'
+
+const testUtilsFunctionsList: OnPreBuild = function ({
+  utils: {
+    functions: { list },
+  },
+}: {
+  utils: NetlifyPluginUtils
+}) {
+  expectType<Promise<ListedFunction[]>>(list())
+}
+
+const testUtilsListAll: OnPreBuild = function ({
+  utils: {
+    functions: { listAll },
+  },
+}: {
+  utils: NetlifyPluginUtils
+}) {
+  expectType<Promise<ListedFunctionFile[]>>(listAll())
+}
+
+const testUtilsFunctionsAdd: OnPreBuild = function ({
+  utils: {
+    functions: { add },
+  },
+}: {
+  utils: NetlifyPluginUtils
+}) {
+  expectType<Promise<void>>(add('functionName'))
+}