3.0.114 • Published 1 year ago

@hexlabs/schema-api-ts v3.0.114

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

schema-api-ts

Takes an OpenApi 3.0 specification schema and generates Api classes, Model classes, SDKs to call APIs externally and Mocks to mock APIs. It will also output a simplified definition of the paths and methods in order for you to process separately. (We use it to generate API Gateway stacks in kloudformation-ts)

Features

  1. SDK Generation
  2. API Generation with Routes using http-api-ts
  3. Type Generation
  4. Mock API Generation

Get Started

Here is a simple schema with one endpoint

import {OpenApiSpecificationBuilder, SchemaBuilder} from "../src";

const builder = SchemaBuilder.create();

// Defines our model (a Chicken)
const schemas = builder.add('Chicken', s =>
    s.object(
    { identifier: s.string(), type: s.string(), name: s.string()}
    )
).build();

// Make sure to export a default constant of the schema
export default OpenApiSpecificationBuilder
.create(schemas, { title: 'Chicken Store API', version: '1.0.0'})
.add('paths', o => ({
  '/chicken/{chickenId}': {
    get: {
      operationId: 'getChicken',
      parameters: [
          o.path('chickenId'), // Any Path variables must be included here
          o.query('someQuery'), // A query parameter
          o.query('someOtherQuery', false, true) // A multi string query parameter
      ],
      responses: {
        200: o.response('A Chicken', o.jsonContent('Chicken')) // Chicken here can only be one of the defined entries in the schema
      }
    },
  }
}))
.build();

Usage

Run the following command against your schema and you will get a directory named generated with everything you need.

schema-api-ts generate $(pwd)/schema.ts v=${API_VERSION:-0.1.0}

Then export as a lambda as follows

class MyApi extends ChickenStoreAPI {
    handlers: Partial<ChickenStoreAPIHandlers> = {
        getChicken: async (request, {query, multiQuery, path}) => {
            console.log('Requested chicken with id', path.chickenId);
            const response: Chicken = {type: 'Orpington', name: 'Jimmy', identifier: '1'};
            return {statusCode: 200, body: JSON.stringify(response)}
        }
    };
}

export const handler = async (event: APIGatewayProxyEvent) {
    const handle = new MyApi().routes();
    //Add filtering in here if needed (https://github.com/hexlabsio/http-api-ts)
    return await handle(event);
}
3.0.114

1 year ago

3.0.112

1 year ago

2.0.109

2 years ago

2.0.111

2 years ago

2.0.110

2 years ago

2.0.108

2 years ago

2.0.107

2 years ago

2.0.106

2 years ago

2.0.105

2 years ago

2.0.104

2 years ago

2.0.103

2 years ago

2.0.102

2 years ago

2.0.101

2 years ago

2.0.100

2 years ago

2.0.99

2 years ago

2.0.97

2 years ago

2.0.98

2 years ago

2.0.95

2 years ago

1.1.94

3 years ago

1.1.93

3 years ago

1.1.92

3 years ago

1.1.91

3 years ago

1.1.90

3 years ago

1.1.88

3 years ago

1.1.85

3 years ago

1.1.84

3 years ago

1.1.83

3 years ago

1.1.82

3 years ago

1.1.87

3 years ago

1.1.86

3 years ago

1.1.78

4 years ago

1.1.77

4 years ago

1.1.79

4 years ago

1.1.81

4 years ago

1.1.80

4 years ago

1.1.76

4 years ago

1.1.74

4 years ago

1.1.75

4 years ago

1.1.70

4 years ago

1.1.73

4 years ago

1.1.72

4 years ago

1.1.71

4 years ago

1.1.69

4 years ago

1.1.68

4 years ago

1.1.67

4 years ago

1.1.63

4 years ago

1.1.62

4 years ago

1.1.61

4 years ago

1.1.66

4 years ago

1.1.65

4 years ago

1.1.64

4 years ago

1.1.29

4 years ago

1.1.28

4 years ago

1.1.30

4 years ago

1.1.34

4 years ago

1.1.33

4 years ago

1.1.32

4 years ago

1.1.31

4 years ago

1.1.38

4 years ago

1.1.37

4 years ago

1.1.36

4 years ago

1.1.35

4 years ago

1.1.39

4 years ago

1.1.41

4 years ago

1.1.40

4 years ago

1.1.45

4 years ago

1.1.44

4 years ago

1.1.43

4 years ago

1.1.42

4 years ago

1.1.49

4 years ago

1.1.48

4 years ago

1.1.47

4 years ago

1.1.46

4 years ago

1.1.52

4 years ago

1.1.51

4 years ago

1.1.50

4 years ago

1.1.56

4 years ago

1.1.55

4 years ago

1.1.54

4 years ago

1.1.53

4 years ago

1.1.59

4 years ago

1.1.58

4 years ago

1.1.60

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago