Commit Graph

88 Commits

Author SHA1 Message Date
Subramanian Krishnan 40f0690573 Make the app instance available to connectors 2016-08-29 15:15:53 +02:00
Miroslav Bajtoš 39da31bb5a test: fix "socket hang up" error in app.test
Rework the test to always wait for the client request to finish before
calling the test done.
2016-08-04 15:51:53 +02:00
Miroslav Bajtoš caaa296a82 test: make status test more robust
Rework assertions to report helpful messages on failure.

Increase the "elapsed" limit from 100ms to 300ms to support our
slow CI machines.
2016-08-04 11:00:00 +02:00
David Cheung ddb5327e64 Update tests for strong-error-handler
Fix rest-adapter related test case switching to strong-error-handler
Only affect the test-cases calling rest methods
2016-06-07 13:26:18 -04:00
Supasate Choochaisri 04e26fae5c Separate error-checking and next/done logic from other logic in the test suite
Signed-off-by: Supasate Choochaisri <supasate.c@gmail.com>
2016-05-05 11:12:48 +07:00
Ryan Graham 6964914bab
update copyright statements 2016-05-03 15:50:21 -07:00
Supasate Choochaisri 0ca24389b2 Add new feature to emit a `remoteMethodDisabled` event when disabling a remote method.
Signed-off-by: Supasate Choochaisri <supasate.c@gmail.com>
2016-04-28 09:03:54 +07:00
Miroslav Bajtoš f9702b0ace Use eslint with loopback config
Drop jshint and jscs in favour of eslint.

Fix style violations.

While we are at this, reduce the max line length from 150 to 100.
2016-04-06 10:45:30 +02:00
Miroslav Bajtoš 93d487ffc0 Improve error message on connector init error 2016-02-26 14:46:26 +01:00
Raymond Feng c44a19dc73 Allow methods filter for middleware config 2015-08-05 10:30:57 -07:00
Miroslav Bajtoš 3115e5055f Auto-configure models required by `app.enableAuth`
Modify `app.enableAuth` to automaticaly setup all required models
that are not attached to the app nor a datasource.

Users wishing to use this option must provide the name of the
data-source to use for these models.

Example usage:

    var app = loopback();
    app.dataSource('db', { connector: 'memory' });
    app.enableAuth({ dataSource: 'db' });

    app.use(loopback.rest());
    app.listen(3000);
2015-06-01 12:19:28 +02:00
Raymond Feng f798882e9a Allow dataSource === false 2015-04-24 08:32:17 -07:00
Ritchie Martori b9170751bc Add support for app level Model isolation
- `loopback.registry` is now a true global registry
 - `app.registry` is unique per app object
 - `Model.registry` is set when a Model is created using any registry method
 - `loopback.localRegistry` and `loopback({localRegistry: true})` when set to `true` this will create a `Registry` per `Application`. It defaults to `false`.
2015-04-03 11:48:45 -07:00
Raymond Feng 548cb6ef94 Fix style issues 2015-03-27 08:59:11 -07:00
Raymond Feng 2ce1d14a0e Upgrade deps 2015-03-04 16:30:03 -08:00
Raymond Feng 9a0267e87a Add a workaround to avoid conflicts with NewRelic 2015-02-25 09:47:18 -08:00
Miroslav Bajtoš 4744aa6920 server-app: make _sortLayersByPhase stable
Fix the phase-sorting algorithm to use a stable sorting algorithm,
since the built-in `Array.prototype.sort` is not stable.
2014-12-15 08:14:26 +01:00
Miroslav Bajtoš 84af4194fb Rework phased middleware, fix several bugs
Bugs fixed:

 - express helpers like `req.get` are now available in middleware
   handlers registered via `app.middleware`

 - `req.url` does not include the mountpath prefix now, this is
   consistent with the behaviour of `app.use`

The implementation of phased middleware was completely rewritten.
 - We no longer use Phase and PhaseList objects from loopback-phase.
 - Handler functions are registered via the `Layer` mechanism used by
   express router.
 - The app keeps the layers sorted according to phases.
2014-12-12 13:25:35 +01:00
Rob Halff 36e1f6840c fix jscs errors 2014-11-21 03:35:36 +01:00
Rob Halff 918497c365 singlequote, semicolon & /*jshint -W030 */ 2014-11-21 02:46:21 +01:00
Miroslav Bajtoš 2baa4b03a3 Scope app middleware to a list of paths
Add a new argument to `app.middleware` allowing developers
to restrict the middleware to a list of paths or regular expresions.

Modify `app.middlewareFromConfig` to pass `config.paths` as the second
arg of `app.middleware`.

Examples:

    // A string path (interpreted via path-to-regexp)
    app.middleware('auth', '/admin', ldapAuth);

    // A regular expression
    app.middleware('initial', /^\/~(admin|root)/, rejectWith404);

    // A list of scopes
    app.middleware('routes', ['/api', /^\/assets/.*\.json$/], foo);

    // From config
    app.middlewareFromConfig(
      handlerFactory,
      {
        phase: 'initial',
        paths: ['/scope', /^\/(a|b)/]
      });
2014-11-19 15:42:54 +01:00
Miroslav Bajtoš 7581ccf260 Merge pull request #796 from strongloop/feature/cleanup-middleware-config-opts
Cleanup middleware config opts
2014-11-18 19:10:07 +01:00
Miroslav Bajtoš b2d9f662e5 Merge pull request #792 from strongloop/feature/allow-serial-phase
app.middleware: verify serial exec of handlers
2014-11-14 18:37:28 +01:00
Miroslav Bajtoš 7647339675 server-app: middleware API improvements
- Rename `config.config` to `config.params`
 - Modify methods to return `this` (fluent API)
2014-11-14 09:52:26 +01:00
Miroslav Bajtoš aa92412db3 app.middleware: verify serial exec of handlers
Add a test verifying that middleware handlers are executed serially.
2014-11-12 10:09:20 +01:00
Miroslav Bajtoš ae7d99682b Simplify `app.defineMiddlewarePhases`
Refactor the implementation to use the new method `phaseList.zipMerge`.

This is commit is changing the behaviour in the case when
the first new phase does not exist in the current list.

Before the change, all new phases were added just before the "routes"
phase.

After this change, new phases are added to the head of the list,
until an existing phase is encountered, at which point the regular
merge algorithm kicks in.

Example:

    app.defineMiddlewarePhases(['first', 'routes', 'subapps']);

Before the change: code throws an error - 'routes' already exists.

After the change: phases are merged with the following result:

    'first', 'initial', ..., 'routes', 'subapps', ...
2014-11-12 08:59:56 +01:00
Miroslav Bajtoš 98d439050a Implement `app.defineMiddlewarePhases`
Implement method for registering (new) middleware phases.

 - If all names are new, then the phases are added just before
   the "routes" phase.

  - Otherwise the provided list of names is merged with the existing
   phases in such way that the order of phases is preserved.

Example

    // built-in phases:
    // initial, session, auth, parse, routes, files, final

    app.defineMiddlewarePhases('custom');
    // new list of phases
    //   initial, session, auth, parse,
    //   custom,
    //   routes, files, final

    app.defineMiddlewarePhases([
      'initial', 'postinit', 'preauth', 'routes', 'subapps'
    ]);
    // new list of phases
    //   initial,
    //   postinit, preauth,
    //   session, auth, parse, custom,
    //   routes,
    //   subapps,
    //   files, final
2014-11-11 19:45:37 +01:00
Miroslav Bajtoš 5578d59631 Implement app.middlewareFromConfig
Implement a function registering a middleware using a factory function
and a JSON config.

Example:

    app.middlewareFromConfig(compression, {
      enabled: true,
      phase: 'initial',
      config: {
        threshold: 128
      }
    });
2014-11-11 18:00:19 +01:00
Miroslav Bajtoš 4e1433b519 Middleware phases - initial implementation
Modify the app and router implementation, so that the middleware is
executed in order defined by phases.

Predefined phases:

    'initial', 'session', 'auth', 'parse', 'routes', 'files', 'final'

Methods defined via `app.use`, `app.route` and friends are executed
as the first thing in 'routes' phase.

API usage:

    app.middleware('initial', compression());
    app.middleware('initial:before', serveFavicon());
    app.middleware('files:after', loopback.urlNotFound());
    app.middleware('final:after', errorHandler());

Middleware flavours:

    // regular handler
    function handler(req, res, next) {
      // do stuff
      next();
    }

    // error handler
    function errorHandler(err, req, res, next) {
      // handle error and/or call next
      next(err);
    }
2014-11-10 19:50:58 +01:00
Raymond Feng 67f8b37562 The elapsed time in milliseconds can be 0 (less than 1 ms) 2014-10-27 15:22:17 -07:00
Fabien Franzen 568c8662b4 Support per-model and per-handler remoting options
Allow the developer to pass custom `remoting` options via Model
settings, e.g.

    PersistedModel.extend(
      'MyModel',
      { name: String },
      {
        remoting: { normalizeHttpPath: true }
      });

Also add `options` arg to `app.handler`, this object is passed directly
to strong-remoting handler.
2014-10-22 09:54:15 +02:00
Miroslav Bajtoš 80020eb273 lib/application: improve URL building algo
When running on Unix and no hostname is specified, use `0.0.0.0`
as the hostname instead of `localhost`.

When running on Windows and the hostname is either not specified or
it is `0.0.0.0` or `::`, use `localhost` in the URL. The reason is
that Windows cannot open URLs using `0.0.0.0` as a hostname.
2014-10-20 13:47:24 +02:00
Miroslav Bajtoš 26b67ba757 registry: warn when dataSource is not specified
Modify `registry.configureModel()` to log a warning when `dataSource`
optiont is not specified at all.

Users should provide `dataSource: null` when the model is intentionally
not attached to any data-source.
2014-08-26 18:04:06 +02:00
Ritchie Martori 7dde6466e5 Only validate dataSource when defined (Fixes #482) 2014-08-26 17:49:15 +02:00
Raymond Feng 5bb4cde593 Update test case to remove usage of deprecated express apis 2014-07-26 14:37:13 -07:00
Raymond Feng af26e09845 Emit a 'modelRemoted' event by app.model()
This event will be listened by loopback-explorer so that models remoted
after the explorer is initiated will be added to the api specs
2014-07-24 17:00:27 -07:00
Miroslav Bajtoš eb5ef04b6a Remove `loopback.compat.usePluralNamesForRemoting`
The `usePluralNamesForRemoting` was added in January 2014 for users
upgrading from LoopBack 1.5 or older.
2014-07-21 16:56:46 +02:00
Miroslav Bajtoš 8ea11ea955 Merge branch 'master' into 2.0
Land the pull request #357
> app: update `url` on `listening` event
2014-07-01 16:18:00 +02:00
Miroslav Bajtoš dfa8d7f49f app: update `url` on `listening` event
Most applications report the URL when started (at least the apps we
are scaffolding using loopback-workspace). Constructing the URL in the
loopback core allows us to simplify the templates and reduce the amount
of repeated code.
2014-07-01 14:27:02 +02:00
Miroslav Bajtoš c896c78db0 Remove `app.boot`
Modify `app.boot` to throw an exception, pointing users to the new
module `loopback-boot`.
2014-06-25 14:09:06 +02:00
Miroslav Bajtoš d2517d8236 Merge branch 'master' into 2.0 2014-06-14 09:40:57 +02:00
Miroslav Bajtoš c4db83ad43 Expose loopback as `app.loopback`
The primary intention is to allow loopback plugins to determine
the version of the loopback framework from the `app` object.
2014-06-13 10:34:51 +02:00
Miroslav Bajtoš d21669b844 Merge branch 'master' into 2.0
Conflicts:
	docs.json
	lib/application.js
	lib/loopback.js
	lib/models/data-model.js
	lib/models/model.js
	lib/models/user.js
	lib/registry.js
	package.json
	test/app.test.js
2014-06-13 10:09:25 +02:00
Miroslav Bajtoš fc0fad4a9f Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:

 1. Build models from JSON config, export them for re-use:

  ```js
  var Customer = loopback.createModelFromConfig({
    name: 'Customer',
    base: 'User',
    properties: {
      address: 'string'
    }
  });
  ```

 2. Attach existing models to a dataSource and a loopback app,
    modify certain model aspects like relations:

  ```js
  loopback.configureModel(Customer, {
    dataSource: db,
    relations: { /* ... */ }
  });
  ```

Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:

```js
var Customer = require('./models').Customer;

app.model(Customer, {
  dataSource: 'db',
  relations: { /* ... */ }
});
```

In order to preserve backwards compatibility,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-12 10:41:44 +02:00
Raymond Feng 7769174d58 Tidy up app.model() to remove duplicate & recusrive call 2014-06-09 23:53:01 -07:00
Raymond Feng 815ad53aa4 Register existing model to app.models during app.model() 2014-06-09 16:31:33 -07:00
Miroslav Bajtoš 5f1c3a86eb Merge pull request #304 from strongloop/feature/createModelFromConfig
[2.0] Add createModelFromConfig and configureModel()
2014-06-06 10:27:03 +02:00
Miroslav Bajtoš f844459311 Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:

 1. Build models from JSON config, export them for re-use:

  ```js
  var Customer = loopback.createModelFromConfig({
    name: 'Customer',
    base: 'User',
    properties: {
      address: 'string'
    }
  });
  ```

 2. Attach existing models to a dataSource and a loopback app,
    modify certain model aspects like relations:

  ```js
  loopback.configureModel(Customer, {
    dataSource: db,
    relations: { /* ... */ }
  });
  ```

Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:

```js
var Customer = require('./models').Customer;

app.model(Customer, {
  dataSource: 'db',
  relations: { /* ... */ }
});
```

In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 17:47:28 +02:00
Miroslav Bajtoš ea5b9d16fc Rename DataModel to PersistedModel 2014-06-05 09:56:00 +02:00
Miroslav Bajtoš 26874fc715 Make app.get/app.set available in browser
Implement settings object and methods in browser-express.

Add test/app.test.js to unit-tests run by karma.
2014-06-03 21:32:27 +02:00