Skip to content

settings_preparers

Marcel Kloubert edited this page Feb 11, 2017 · 3 revisions

Home >> Settings >> Preparers

Preparers

Preparers are scripts that can be used to prepare an user / guest account object before it is validated and used.

First define the script in your settings:

{
    "rest.api": {
        // ...

        "preparer": {
            "script": "./my-preparer.js",
            "options": "MKLTM"
        }

        // ...
    }
}

The ./my-preparer.js should look like this:

exports.prepare = function(args) {
    // access VS Code API (s. https://code.visualstudio.com/Docs/extensionAPI/vscode-api)
    var vscode = require('vscode');
    
    // access Node.js API provided by VS Code
    // s.  (s. https://nodejs.org/api/)
    var fs = require('fs');
    
    // access an own module
    var myModule = require('./my-module.js');
    
    // access a module used by the extension:
    // s. https://mkloubert.github.io/vs-rest-api/modules/_helpers_.html
    var helpers = args.require('./helpers');
    // s. https://mkloubert.github.io/vs-rest-api/modules/_host_helpers_.html
    var hostHelpers = args.require('./host/helpers');
    
    // access a module that is part of the extentsion
    // s. https://github.com/mkloubert/vs-rest-api/blob/master/package.json
    var glob = args.require('glob');
    
    // access the data from the settings
    // from the example above this is: "MKLTM"
    var opts = args.options;
    
    // share / store data (while current session)...
    // ... for this script
    var myState = args.state;
    args.state = new Date();
    // ... with other scripts of this type
    args.globalState['myPreparer'] = new Date();
    // ... with the whole workspace
    args.workspaceState['myPreparer'] = new Date();


    // ...

    // work with the object in 'args.user', e.g.
    // s. https://mkloubert.github.io/vs-rest-api/interfaces/_contracts_.user.html
    args.user.set("myValue",
                  "This is value for the user that is stored while the current session");
}

You can return a Promise for async executions or nothing for sync executions.

The args parameter uses the UserPreparerArguments interface.

Clone this wiki locally