1.0.0-beta.4 • Published 4 months ago

netlify-msf v1.0.0-beta.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

netlify-msf

A lightweight framework for building serverless microservices with Netlify Functions.



Installation

You can install netlify-msf using npm, yarn, or pnpm:

npm install netlify-msf
# or
yarn add netlify-msf
# or
pnpm add netlify-msf

CLI Usage

netlify-msf provides a command-line interface to manage your microservice framework.

Usage: netlify-msf [options] [command]

A lightweight framework for building serverless microservices with Netlify Functions.

Options:
  -V, --version   output the version number
  -h, --help      display help for command

Commands:
  openapi-gen     Generate OpenAPI documentation.
  help [command]  display help for command

Configuration

The framework relies on a netlify-msf.config.ts file at the root level of your repository to define API endpoints and OpenAPI documentation.

Example:

// UsersSchema.ts
import { z } from 'zod';

const UserSchema = z.object({
  id: z.number(),
  name: z.string(),
});

const UsersSchema = z.array(UserSchema);

export default UsersSchema;
// netlify-msf.config.ts
import { Config } from 'netlify-msf';
import UsersSchema from './UsersSchema';

const config: Config = {
  schemas: {
    UsersSchema,
  },
  openapi: {
    definition: {
      title: "My microservice",
      description: "Description about my microservice.",
      version: "1.0.0",
      paths: {
        "/users": {
          get: {
            summary: "Get all users",
            responses: {
              200: {
                description: "Success.",
                schema: "UsersSchema",
              },
            },
          },
        },
      },
    },
  },
};

Generating OpenAPI Documentation

Run the following command to generate an openapi.yaml file at the root level or in the specified outputDir:

netlify-msf openapi-gen

API Handler

The library exports a handler function that wraps Netlify functions with Zod-based request validation.

Usage Example

import * as msf from 'netlify-msf';
import { z } from 'zod';

const querySchema = z.object({ id: z.string() });
const bodySchema = z.object({ name: z.string() });

export const handler = msf.handler({ querySchema, bodySchema })((request, event, context) => {
  return {
    message: `Hello, ${request.body.name}!`
  };
});

Error Handling

MsfError is a custom error class for handling API errors gracefully.

Example:

import { MsfError } from 'netlify-msf';

throw new MsfError(400, "Invalid request");

License

MIT