1.8.1 • Published 2 months ago

@proxymal/saas-sdk v1.8.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

saas-sdk

SDK for building all types of SaaS products.

Rest API

SDK allows you to build Rest API from scratch easily, with express-like syntax:

// app.ts

import { API } from "@proxymal/saas-sdk";
import user from "./user.routes.ts";

const api = new API();

api.expand("/api/user", user);

api.start(3000);
// user.routes.ts

import { APIModule } from "@proxymal/saas-sdk";

const routes = new APIModule();

// GET /
routes.get("/", {}, async () => ({ status: "ok", body: { test: "hello!" } }));

export default routes;

Rest API Documentation

Documentation for your Rest API can be automatically generated (OpenAPI v3 schema format) using Docs:

// app.ts

import { API, Docs } from "@proxymal/saas-sdk";
import user from "./user.routes.ts";

const api = new API();

api.expand("/api/user", user);
api.expand("/api/docs", new Docs(api));

api.start(3000);

Database & ORM

Database is works out-of-box by default, you just have to setup .env file with connection credentials:

DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=username
DB_PASSWORD=password
DB_DATABASE=database

And initialize database:

// db.ts

import { Database } from "@proxymal/saas-sdk";
import { User } from "./user.entity.ts";

const db = new Database([User]);
export default db;

With some TypeORM entity:

// user.entity.ts

import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from "typeorm";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    email: string;

    @Column()
    password_hash: string;

    @CreateDateColumn()
    create_date: Date;
}

Also don't forget to add connection to the database:

// app.ts

import { API, Docs } from "@proxymal/saas-sdk";
import db from "./db.ts";
import user from "./user.routes.ts";

const api = new API();

api.expand("/api/user", user);

api.start(3000, async () => await db.init());

Entity Services API

As you connected the database and defined entities schema, you can create entity-based service for working with entity database table:

// user.service.ts

import { EntityBaseService } from "@proxymal/saas-sdk";
import { EntityTarget, ObjectLiteral } from "typeorm";
import db from "./db";
import { User } from "./user.entity.ts";

export const UserService = new (class extends EntityBaseService<User> {
    constructor(entity: EntityTarget<ObjectLiteral>) {
        super(db, entity);
    }
})(User);

Now you can work with this entity anywhere:

import { UserService } from "./user.service.ts";

// Find one row of user by it's `id`
await UserService.findOneBy({ id: 1 });

Auth API (JWT)

CRUD API

Logging API

HTML Components API

HTML Pages API (WIP)

You have to build multi-page SSR website? You can use HTML Pages API for that!

Initialize SSR rendering globally (like db.ts):

// ssr.ts

import { SSR } from "@proxymal/saas-sdk";

const ssr = new SSR({
    meta: {
        title: "SaaS",
        description: "SaaS service",
    },
    styles: ["./styles/fonts.css", "./styles/global.css", "./styles/theme.css"],
});

export default ssr;

After that define a page markup:

<!-- /pages/index.page.html -->

<meta> { "title": "SaaS – Main page" } </meta>

<template>
    <section>
        <h1>{{ title }}</h1>
    </section>
</template>

<style>
    section {
        width: 100%;

        display: flex;
    }

    h1 {
        font-size: 52px;
    }
</style>

And render your page in some route:

// website.routes.ts

import { APIModule, SSR } from "@proxymal/saas-sdk";
import ssr from "./ssr.ts";
import index from "./pages/index.page.html";

const routes = new APIModule();

// GET /
routes.get("/", {}, async () => {
    return ssr.render(index, {
        title: "Hello world",
    });
});

export default routes;

NOTE: if you are using direct import of .html files (import "file.html"), you should setup in your .d.ts file this:

declare module "*.html" {
    const value: string;
    export default value;
}
1.8.1

2 months ago

1.8.0

2 months ago

1.7.9

2 months ago

1.7.8

2 months ago

1.7.7

2 months ago

1.7.6

2 months ago

1.7.5

2 months ago

1.7.4

2 months ago

1.7.3

2 months ago

1.7.2

2 months ago

1.7.1

2 months ago

1.7.0

2 months ago

1.6.9

2 months ago

1.6.8

2 months ago

1.6.7

2 months ago

1.6.6

2 months ago

1.6.4

2 months ago

1.6.5

2 months ago

1.6.3

2 months ago

1.6.2

3 months ago

1.6.1

3 months ago

1.6.0

3 months ago

1.5.9

3 months ago

1.5.5

3 months ago

1.4.6

3 months ago

1.5.4

3 months ago

1.4.5

3 months ago

1.5.3

3 months ago

1.4.4

3 months ago

1.5.2

3 months ago

1.5.1

3 months ago

1.5.0

3 months ago

1.5.8

3 months ago

1.4.9

3 months ago

1.5.7

3 months ago

1.4.8

3 months ago

1.5.6

3 months ago

1.4.7

3 months ago

1.4.3

3 months ago

1.4.2

3 months ago

1.4.1

5 months ago

1.4.0

5 months ago

1.3.2

5 months ago

1.3.1

6 months ago

1.3.0

6 months ago

1.2.9

7 months ago

1.2.8

7 months ago

1.2.7

7 months ago

1.2.6

7 months ago

1.2.5

7 months ago

1.2.4

7 months ago

1.2.3

8 months ago

1.2.2

8 months ago

1.2.1

8 months ago

1.2.0

8 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.0

9 months ago