nitrictech / node-sdk

Node SDK for Nitric
https://nitric.io
Apache License 2.0
66 stars 4 forks source link

Default API #147

Open jyecusch opened 2 years ago

jyecusch commented 2 years ago

Feature Request

Suggestion

Many applications only include a single API gateway, particularly when developers are learning the framework. To make this use case easier let's expose a 'default' API to reduce the need to create one in every app.

Functions would change from this:

import { api } from "@nitric/sdk";

const helloApi = api('main');

helloApi.get("/hello/:name", async (ctx) => {
    const { name } = ctx.req.params;
    ctx.res.body = `Hello ${name}`;
});

To this:

import { api } from "@nitric/sdk";

api.get("/hello/:name", async (ctx) => {
    const { name } = ctx.req.params;
    ctx.res.body = `Hello ${name}`;
});

Then, creating additional APIs could be performed by calling the api object (same as today):

const main = api('main');

Alternatively, if that's confusing or not possible we could add a new method, e.g.:

const main = api.new('main');

Note: this would be a breaking change.

Value

Should be less confusing for developers new to the framework and requires less code for the majority of projects that will only have a single API.

tjholm commented 1 year ago

One thing that needs to be considered here is securing the default API.

This is currently defined upon API creation, as it needs to be defined at the top level of the API.

Currently we do:

import { api, jwt } from "@nitric/sdk";

const publicApi = api('public', {
  securityDefinitions: {
     'user': jwt({
        issuer: "example...",
        audiences: [],
     })
  }
});

We would need a way to define this for the default api as well.

something like

import { api } from "@nitric/sdk";

api.withSecurity(...)

Also need to somehow invalidate the make cache or force a re-application or delay creation of the API resource.