FunctionScript将 JavaScript 函数转为类型化的 HTTP API

联合创作 · 2023-09-22 02:24

FunctionScript 是一种将 JavaScript 函数转为类型化 HTTP API 的语言和规范。

它允许 JavaScript(Node.js)函数通过对 HTTP 接口进行定义(包括类型安全机制),从而无缝导出为 HTTP API。

示例代码

/**
* Select Rows from a Spreadsheet by querying it like a Database
* @param {string} spreadsheetId The id of the Spreadsheet.
* @param {string} range The A1 notation of the values to use as a table.
* @param {enum} bounds Specify the ending bounds of the table.
*   ["FIRST_EMPTY_ROW", "FIRST_EMPTY_ROW"]
*   ["FULL_RANGE", "FULL_RANGE"]
* @param {object} where A list of column values to filter by.
* @param {object} limit A limit representing the number of results to return
* @ {number} offset The offset of records to begin at
* @ {number} count The number of records to return, 0 will return all
* @returns {object} selectQueryResult
* @ {string} spreadsheetId
* @ {string} range
* @ {array} rows An array of objects corresponding to row values
*/
module.exports = async (
  spreadsheetId = null,
  range,
  bounds = 'FIRST_EMPTY_ROW',
  where = {},
  limit = {offset: 0, count: 0},
  context
) => {

  /* implementation-specific JavaScript */

  return {/* some data */};

};

上面的代码将会生成一个 API:

  • spreadsheetId A string
  • range A string
  • bounds An enum, can be either "FIRST_EMPTY_ROW" or "FULL_RANGE"
  • where An object
  • limit An object that must contain:
    • limit.offset, a number
    • limit.count, a number

返回一个对象:

selectQueryResult

  • selectQueryResult.spreadsheetId must be a string
  • selectQueryResult.range must be a string
  • selectQueryResult.rows must be an array
浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报