Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TypeScript with Sails

Is anyone using TypeScript with Sails? If so, what are you using for external declarations?

I am in the middle of a Sails application project and learned about TypeScript. I am trying to determine if TypeScript is something I should pursue for this project. Any info will be appreciated. Thanks.

like image 413
Laurence Drews Avatar asked Jan 12 '15 01:01

Laurence Drews


2 Answers

from : https://github.com/balderdashy/sails Sails is built with node connect express and socket.io. This means that definition files for these libraries are what you need. All of these are available from the Definitive TypeScript definition repository : https://github.com/borisyankov/DefinitelyTyped.

like image 173
basarat Avatar answered Oct 28 '22 22:10

basarat


typings.json

{
  "dependencies": {
    "bluebird": "registry:npm/bluebird#3.5.0+20170314181206",
    "connect": "registry:dt/connect#3.4.0+20160510010627",
    "express": "registry:dt/express#4.0.0+20170118060322",
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20170324160323",
    "sails": "registry:npm/sails#0.12.0+20160610190623",
    "serve-static": "registry:dt/serve-static#1.7.1+20161128184045"
  },
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160726072212",
    "node": "registry:dt/node#7.0.0+20170322231424",
    "socket.io": "registry:dt/socket.io#1.4.4+20170313110830"
  }
}

welcome controller

   /**
     * WelcomeController.ts
     *
     * @description :: Server-side logic for managing Welcomes in TS
     * @help        :: See http://links.sailsjs.org/docs/controllers
     */

    import e = require('express');
    import util = require('util');

    declare const sails: any;

    const WelcomeController = {
      index: function (req: e.Request, res: e.Response, next: Function) {
        console.log('index() from WelcomeController.ts');
        sails.models.welcome.find().limit(1).then((welcome) => {
          /// TODO: add logger
          console.log(`welcome page rendering w/ message ${welcome[0].message}`);
          return res.render('welcome', {
            welcome: welcome[0].message
          });
        }).catch((err:Error) => {
          console.error(err.message);
          return res.render('500', err)
        });


      },
      config: function (req: e.Request, res:e.Response, next:Function) {
        console.log('config() from WelcomeController.ts');
        return res.status(200)
          .send('<h1>sails.config :</h1><pre>' + util.inspect(sails.config) + '<pre>');
      }
    };

    module.exports = WelcomeController;

model

export class Welcome {
   attributes: any = {
    id: {
      type: 'integer',
      primaryKey: true
    },
    message: {
      type: 'string',
      required: true,
      defaultsTo: 'default message'
    }
  };
}

View

<div class="default-page">
  <div class="header">
    <h1 id="main-title" class="container"></h1>
    <h3 class="container">Message: <code><%= welcome %></code></h3>
  </div>
  <div class="main container clearfix">

and so on... I started an example project on git but never finished:

https://github.com/aslanvaroqua/sails-ts

like image 43
Aslan Varoqua Avatar answered Oct 28 '22 20:10

Aslan Varoqua