@proxymal/saas-sdk
v1.8.5
Published
SDK for building all types of SaaS products.
Downloads
94
Maintainers
Readme
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;
}