Skip to content

OpenAPIs using remote functions #15080

@ixxie

Description

@ixxie

Describe the problem

Remote functions are a blast to work with in SvelteKit. But of course, there is a whole world out there beyond SvelteKit. So often one would want to expose a REST API. OpenAPI is a well established standard, not only providing automated documentation solutions, but also automated SDK generation in many languages.

Describe the proposed solution

Since remote functions already compile to endpoints, I wondered if it may be feasible to support generating REST APIs like this:

// src/openapi.server.ts
import { 
  createFooCommand,
  readFooQuery,
  updateFooCommand,
  deleteFooCommand
} from '$lib/remotes/foo.remote.ts';
import { 
  createBarCommand,
  readBarQuery,
  updateBarCommand,
  deleteBarCommand
} from '$lib/remotes/bar.remote.ts';

export const spec = {
  foo: {
    POST: createFooCommand
       .endpoint({
           // additional options passed here
       }),
    GET: readFooCommand,
    PUT: updateFooCommand
    DELETE: deleteFooCommand
  },
  bar: {
     // etc
  }
}

Alternatives considered

Currently, as far as I know, one can either:

  1. Manually write endpoint wrappers for remote functions and somehow generate and serve an openapi.json file
  2. Use a fallback method handler to dynamically generate the full API and serve the openapi.json

Importance

would make my life easier

Additional Information

I'm not exactly sure how this sort of proposal plays with guards such as proposed here: #14612

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions