ryan
ryan

Reputation: 6655

Require.js singleton to TypeScript

Here are two watered down example files. This is currently how they work as requirejs modules. I've shimmed api.js in my require config to define the export as API. The goal is that these will stay as singletons. I'm trying to convert them to TypeScript equivalents but can't quite figure out how to keep them as a singleton and pass them around to various modules like I do now.

RequireJS style

api.js

(function (global) {

    var API = global.API = {};
    API.version = '0.0.1';
    API.env = 'local';
    API.header = '';

} (this));

auth.js

define([
    'api',
], function (api) {
        'use strict';

        CommonAuth = {

            maxExpiredAuthorizationRetries: 1,

            getAuthorizationHeader: function () {
                return api.header;
            },

            setAuthorizationHeader: function (val) {
                api.header = val;
            }

        };

        return CommonAuth;

    }
);

TypeScript/AMD(requirejs) style

I've got the following so far but this is obviously not what I am after.

auth.ts

// don't know how to fake this without creating a valid api module. requirejs shim is what handles my export
import api = module("api");

export module Common {

    class Auth {

        public maxExpiredAuthorizationRetries: number;

        constructor (maxExpiredAuthorizationRetries: number) {
            this.maxExpiredAuthorizationRetries = maxExpiredAuthorizationRetries;
        }

        public getAuthorizationHeader(): string {
            return api.header();
        }

        public setAuthorizationHeader(val: string): void {
            api.header(val);
        }
    }

    var auth = new Auth(1);
}

which compiles to auth.js (tsc auth.ts --module amd)

define(["require", "exports", "api"], function(require, exports, __api__) {
    var api = __api__;

    (function (Common) {
        var Auth = (function () {
            function Auth(maxExpiredAuthorizationRetries) {
                this.maxExpiredAuthorizationRetries = maxExpiredAuthorizationRetries;
            }
            Auth.prototype.getAuthorizationHeader = function () {
                return api.header();
            };
            Auth.prototype.setAuthorizationHeader = function (val) {
                api.header(val);
            };
            return Auth;
        })();        
        var auth = new Auth(1);
    })(exports.Common || (exports.Common = {}));

})

Update: in addition to the accepted answer you can directly export all members:

export var maxExpiredAuthorizationRetries: number = 1;
export function getAuthorizationHeader(): string { ...
export function setAuthorizationHeader(val: string): string {

and then import api = module('api'); can be used simply by saying api.getAuthorizationHeader().

Upvotes: 4

Views: 2551

Answers (1)

Absolom
Absolom

Reputation: 1389

If you are looking for Singleton, I would use static members in a class. Something like this:

In api.ts

class Api {
    static version:string = '0.0.1';
    static header:string = '';
}

export = Api

In auth.ts

import Api = module("api");

class Auth {

    //...

    public getAuthorizationHeader(): string {
        return Api.header;
    }

    public setAuthorizationHeader(val: string): void {
        Api.header = val;
    }
}

Upvotes: 3

Related Questions