Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aaronpowell/azure-functions-nodejs-openapi
https://github.com/aaronpowell/azure-functions-nodejs-openapi
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/aaronpowell/azure-functions-nodejs-openapi
- Owner: aaronpowell
- License: mit
- Created: 2021-08-02T00:15:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-24T05:22:20.000Z (over 2 years ago)
- Last Synced: 2024-10-19T03:23:43.505Z (3 months ago)
- Language: TypeScript
- Size: 268 KB
- Stars: 24
- Watchers: 8
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README
# OpenAPI for Azure Functions [![Publish a release](https://github.com/aaronpowell/azure-functions-nodejs-openapi/actions/workflows/release.yaml/badge.svg)](https://github.com/aaronpowell/azure-functions-nodejs-openapi/actions/workflows/release.yaml) [![npm version](https://img.shields.io/npm/v/@aaronpowell/azure-functions-nodejs-openapi)](https://www.npmjs.org/package/@aaronpowell/azure-functions-nodejs-openapi)
This is an extension for [Azure Functions](https://azure.microsoft.com/services/functions/?WT.mc_id=javascript-48109-aapowell#overview) that gives support for generating [OpenAPI](https://www.openapis.org/) spec files from annotated Azure Functions. To make this easier, the extension is written in TypeScript and provided TypeScript typings for the objects needing ot be created.
The plugin supports the three major releases of OpenAPI/Swagger, v2, v3 and v3.1, via specific imports, with the default export being OpenAPI 3.1.
The plugin was inspired by [the .NET extension](https://github.com/Azure/azure-functions-openapi-extension).
## Usage
### Step 1 - Annotate an Azure Function
Import the desired version helper to annotate an Azure Function with the metadata for OpenAPI.
```ts
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import { mapOpenApi3_1 } from "@aaronpowell/azure-functions-nodejs-openapi";const httpTrigger: AzureFunction = async function (
context: Context,
req: HttpRequest
): Promise {
context.log("HTTP trigger function processed a request.");
const name = req.query.name;
const responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";context.res = {
body: responseMessage,
};
};export default mapOpenApi3_1(httpTrigger, "/get-message", {
get: {
parameters: [
{
name: "name",
in: "query",
required: true,
schema: {
type: "string",
},
},
],
responses: {
"200": {
description: "Gets a message from the Function",
content: {
"application/json": {
example:
"Hello Aaron. This is a HTTP triggered function executed successfully.",
},
},
},
},
},
});
```For the case where a function contains multiple endpoints, you can pass an array into the ```routes``` and ```specs``` parameters.
```
export default mapOpenApi3_1(httpTrigger, ["/get-message-1", "/get-message-2"], [
{
get: {
...
},
},
{
get: {
...
},
post: {
...
},
}
]);
```### Step 2 - Create the OpenAPI endpoint
Create a new HTTP Trigger Azure Functions and name it how you want it exposed to consumers (eg `swagger`), and import the function to generate the spec file:
```ts
import { generateOpenApi3_1Spec } from "@aaronpowell/azure-functions-nodejs-openapi";export default generateOpenApi3_1Spec({
info: {
title: "Azure Function Swagger v3.1 demo",
version: "1.0.0",
},
});
```**Note: You'll need to edit the `function.json` to have the `out` parameter named `res`, as the generator function will assign it to that on `context`.**
## Examples
You find examples for the different OpenAPI version in the folder `example`. They are running on Azure Functions Runtime version 3.x.
If you want to run them on version 4.x, adjust the extension bundle reference in the `host.json` to:```json
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
```## Scaffolding Function descriptions
_The following is very much a work in progress._
To get started in generating the OpenAPI annotations for Functions, there's a CLI tool provided as part of this package, `openapi-func`.
This will look at the `function.json` for a Function and give some of the boilerplate required to get you started.
```bash
cd example/trivia-app
npx openapi-func endpoint -f ./game-get
```This will output the following for you to add to your Function:
```
Here's a starting point for your OpenAPI enabled FunctionChange the default export from the Azure Function to the following:
export default mapOpenApi3_1(httpTrigger, "/game-get", {
get: {
responses: { '200': { description: 'The response from the Azure Function' } }
}
})
```## License
MIT