How to use typescript-rest - 10 common examples

To help you get started, we’ve selected a few typescript-rest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vellengs / typerx / dist / controllers / account-controller.js View on Github external
return __awaiter(this, void 0, void 0, function* () {
            throw new typescript_rest_1.Errors.UnauthorizedError('没有登录');
            // return null;
        });
    }
github vellengs / typerx / packages / server / dist / modules / core / account.service.js View on Github external
return __awaiter(this, void 0, void 0, function* () {
            if (admin && admin.isAdmin) {
                // this.setKeyWord(entry);
                const doc = yield core_database_1.CoreDatabase.Account.findOneAndUpdate({
                    _id: entry.id,
                }, entry).exec();
                return doc;
            }
            else {
                throw new typescript_rest_1.Errors.ForbiddenError('禁止非管理员更新帐号信息!');
            }
        });
    }
github vellengs / typerx / dist / api-server.js View on Github external
constructor() {
        this.server = null;
        this.PORT = parseInt(process.env.PORT, 0) || 3600;
        this.app = express();
        this.config();
        mongoose.connect(config.db, {
            useMongoClient: true,
        });
        const db = mongoose.connection;
        autoIncrement.initialize(db);
        db.on('error', (err) => {
            throw new Error('unable to connect to database at ' + config.db + err);
        });
        typescript_rest_1.Server.buildServices(this.app, ...controllers_1.default);
        // TODO: enable for Swagger generation error
        // Server.loadServices(this.app, 'controllers/*', __dirname);
        typescript_rest_1.Server.swagger(this.app, './dist/swagger.json', '/api-docs', 'localhost:3600', ['http']);
        this.app.use((err, req, res, next) => {
            if (res.headersSent) {
                return next(err);
            }
            if (err && err.statusCode) {
                res.status(err.statusCode);
            }
            else {
                res.status(500);
            }
            res.send({ error: err });
        });
    }
github vellengs / typerx / dist / api-server.js View on Github external
this.server = null;
        this.PORT = parseInt(process.env.PORT, 0) || 3600;
        this.app = express();
        this.config();
        mongoose.connect(config.db, {
            useMongoClient: true,
        });
        const db = mongoose.connection;
        autoIncrement.initialize(db);
        db.on('error', (err) => {
            throw new Error('unable to connect to database at ' + config.db + err);
        });
        typescript_rest_1.Server.buildServices(this.app, ...controllers_1.default);
        // TODO: enable for Swagger generation error
        // Server.loadServices(this.app, 'controllers/*', __dirname);
        typescript_rest_1.Server.swagger(this.app, './dist/swagger.json', '/api-docs', 'localhost:3600', ['http']);
        this.app.use((err, req, res, next) => {
            if (res.headersSent) {
                return next(err);
            }
            if (err && err.statusCode) {
                res.status(err.statusCode);
            }
            else {
                res.status(500);
            }
            res.send({ error: err });
        });
    }
    /**
github vellengs / typerx / server / api.server.ts View on Github external
constructor() {
        this.app = express();
        this.config();

        mongoose.connect(config.db, {
            useMongoClient: true,
        });
        const db = mongoose.connection;
        autoIncrement.initialize(db);

        db.on('error', (err: any) => {
            throw new Error('unable to connect to database at ' + config.db + err);
        });

        Server.buildServices(this.app, ...controllers);
        // TODO: enable for Swagger generation error
        // Server.loadServices(this.app, 'controllers/*', __dirname);
        Server.swagger(this.app, './dist/swagger.json', '/api-docs', 'localhost:' + this.PORT, ['http']);

        this.app.use((
            err: any,
            req: express.Request,
            res: express.Response, next: any) => {
            if (res.headersSent) {
                return next(err);
            }
            if (err && err.statusCode) {
                res.status(err.statusCode);
            } else {
                res.status(500);
            }
github vellengs / typerx / server / api.server.ts View on Github external
this.config();

        mongoose.connect(config.db, {
            useMongoClient: true,
        });
        const db = mongoose.connection;
        autoIncrement.initialize(db);

        db.on('error', (err: any) => {
            throw new Error('unable to connect to database at ' + config.db + err);
        });

        Server.buildServices(this.app, ...controllers);
        // TODO: enable for Swagger generation error
        // Server.loadServices(this.app, 'controllers/*', __dirname);
        Server.swagger(this.app, './dist/swagger.json', '/api-docs', 'localhost:' + this.PORT, ['http']);

        this.app.use((
            err: any,
            req: express.Request,
            res: express.Response, next: any) => {
            if (res.headersSent) {
                return next(err);
            }
            if (err && err.statusCode) {
                res.status(err.statusCode);
            } else {
                res.status(500);
            }
            res.send({ error: err });
        });
    }
github turt2live / matrix-dimension / src / api / Webserver.ts View on Github external
private loadRoutes() {
        // TODO: Rename services to controllers, and controllers to services. They're backwards.

        const apis = ["scalar", "dimension", "admin", "matrix"].map(a => path.join(__dirname, a, "*.js"));
        const router = express.Router();
        Server.useIoC();
        Server.registerAuthenticator(new MatrixSecurity());
        apis.forEach(a => Server.loadServices(router, [a]));
        const routes = _.uniq(router.stack.map(r => r.route.path));
        for (const route of routes) {
            this.app.options(route, (_req, res) => res.sendStatus(200));
            LogService.info("Webserver", "Registered route: " + route);
        }
        this.app.use(router);

        // We register the default route last to make sure we don't override anything by accident.
        // We'll pass off all other requests to the web app
        this.app.get(/(widgets\/|riot\/|\/)*/, (_req, res) => {
            res.sendFile(path.join(__dirname, "..", "..", "web", "index.html"));
        });

        // Set up the error handler
github thiagobustamante / typescript-rest-swagger / test / data / apis.ts View on Github external
export class NamedEntity implements Entity {
    public id: number;
    public name: string;
}

@Path('abstract')
export class AbstractEntityEndpoint {
    @GET
    public get(): NamedEntity {
        return new NamedEntity();
    }
}

@Path('secure')
@Security(['ROLE_1', 'ROLE_2'], 'access_token')
export class SecureEndpoint {
    @GET
    public get(): string {
        return 'Access Granted';
    }

    @POST
    @Security([], 'user_email')
    public post(): string {
        return 'Posted';
    }
}

@Path('supersecure')
@Security('access_token')
@Security('user_email')
github thiagobustamante / typescript-rest-swagger / test / data / apis.ts View on Github external
export class SecureEndpoint {
    @GET
    public get(): string {
        return 'Access Granted';
    }

    @POST
    @Security([], 'user_email')
    public post(): string {
        return 'Posted';
    }
}

@Path('supersecure')
@Security('access_token')
@Security('user_email')
@Security()
export class SuperSecureEndpoint {
    @GET
    public get(): string {
        return 'Access Granted';
    }
}

@Path('response')
@swagger.Response(400, 'The request format was incorrect.')
@swagger.Response(500, 'There was an unexpected error.')
export class ResponseController {
    @GET
    public get(): string {
        return '42';
    }
github thiagobustamante / typescript-rest-swagger / test / data / apis.ts View on Github external
@GET
    public get(): string {
        return 'Access Granted';
    }

    @POST
    @Security([], 'user_email')
    public post(): string {
        return 'Posted';
    }
}

@Path('supersecure')
@Security('access_token')
@Security('user_email')
@Security()
export class SuperSecureEndpoint {
    @GET
    public get(): string {
        return 'Access Granted';
    }
}

@Path('response')
@swagger.Response(400, 'The request format was incorrect.')
@swagger.Response(500, 'There was an unexpected error.')
export class ResponseController {
    @GET
    public get(): string {
        return '42';
    }