From 10452eb0788e99f5ba56dbc61e4fc7d4526f09e0 Mon Sep 17 00:00:00 2001 From: Mike Crowe Date: Tue, 12 Sep 2017 15:12:50 -0400 Subject: [PATCH] Added 'static' to Model.remoteMethod Integrated the remoteMethod ideas from https://github.com/Sequoia/loopback-type-definitions/blob/master/loopback.d.ts into the existing type Improves remoteMethod composition and checking. No impact on other types --- types/loopback/index.d.ts | 162 +++++++++++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 1 deletion(-) diff --git a/types/loopback/index.d.ts b/types/loopback/index.d.ts index 0fd42bde9d..49ee739f9e 100644 --- a/types/loopback/index.d.ts +++ b/types/loopback/index.d.ts @@ -2,6 +2,8 @@ // Project: https://github.com/strongloop/loopback // Definitions by: Andres D Jimenez // Tim Schumacher +// Sequoia McDowell +// Mike Crowe // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -723,6 +725,164 @@ declare namespace l { equals(p: any): void; } + /** + * @interface + * @property {string} path HTTP path (relative to the model) at which the method is exposed. + * @property {'get' | 'post' | 'patch' | 'put' | 'del' | 'all'} verb HTTP method (verb) at which the method is available. + * @property {number} status Default HTTP status set when the callback is called without an error. + * @property {number} errorStatus Default HTTP status set when the callback is called with an error. + */ + interface RemoteHttpOptions { + /** + * HTTP path (relative to the model) at which the method is exposed. + * ``` + * http: {path: '/sayhi'} + * ``` + */ + path: string; + /** + * HTTP method (verb) at which the method is available. + * ``` + * http: {path: '/sayhi', verb: 'get'} + * ``` + * default = post + */ + verb: 'get' | 'post' | 'patch' | 'put' | 'del' | 'all'; + + /** + * status Default HTTP status set when the callback is called without an error. + * ``` + * {status: 201} + * ``` + */ + status?: number; + + /** + * errorStatus Default HTTP status set when the callback is called with an error. + * ``` + * {errorStatus: 400} + * ``` + */ + errorStatus?: number; + } + + /** + * @interface + * @property {Array} accepts Defines arguments that the remote method accepts + * @property {string|string[]} description Text description of the method + * @property {l.RemoteHttpOptions} http The HTTP options for the remote method + * @property {boolean} isStatic Whether the method is static (eg. `MyModel.myMethod`) + * @property {string | string[]} notes Additional notes, used by API documentation generators like Swagger. + * @property {l.RemoteMethodArgument} returns Describes the remote method's callback arguments + */ + interface RemoteMethodOptions { + /** + * Defines arguments that the remote method accepts. These arguments map to the static method you define. For the example above, you can see the function signature: + Person.greet(name, age, callback)... + `name` is the first argument, `age` is the second argument and callback is automatically provided by LoopBack (do not specify it in your `accepts` array). For more info, see Argument descriptions. + Default if not provided is the empty array, []. + { ... + accepts: [ + {arg: 'name', type: 'string'}, + {arg: 'age', type: 'number'},...], + ... } + */ + accepts?: Array; + + /** + * Text description of the method, used by API documentation generators such as Swagger. + You can put long strings in an array if needed (see note below). + */ + description?: string|string[]; + + /** + * + */ + http?: l.RemoteHttpOptions; + + /** + * + * boolean. Whether the method is static (eg. `MyModel.myMethod`). Use `false` to define the method on the prototype (for example, `MyModel.prototype.myMethod`). Default is true. + * default: true + */ + isStatic?: boolean; + + /** + * + Additional notes, used by API documentation generators like Swagger. + You can put long strings in an array if needed (see note below). + */ + notes? : string | string[]; + + /** + * Describes the remote method's callback arguments; See Argument descriptions. The err argument is assumed; do not specify. + * Default if not provided is the empty array, []. + * ``` + * returns: {arg: 'greeting', type: 'string'}` + * ``` + */ + returns?: l.RemoteMethodArgument; + } + + /** + * @interface + * @property {string} arg Argument name + * @property {string | string[]} description A text description of the argument. + * @property {any} http http Object or Function For input arguments: a function or an object describing mapping from HTTP request to the argument value. + * @property {boolean} required True if argument is required; false otherwise. + * @property {boolean} root For callback arguments: set this property to true if your function has a single callback argument to use as the root object returned to remote caller. Otherwise the root object returned is a map (argument-name to argument-value). + * @property {"any" | "Array" | "Boolean" | "Buffer" | "Date" | "GeoPoint" | "null" | "Number" | "Object" | "String"} type + * @property {string} default Default value that will be used to populate loopback-explorer input fields and swagger documentation + */ + interface RemoteMethodArgument { + /** + * Argument name + */ + arg: string; + /** + * A text description of the argument. This is used by API documentation generators like Swagger. + You can split long descriptions into arrays of strings (lines) to keep line lengths manageable. + ``` + [ + "Lorem ipsum dolor sit amet, consectetur adipiscing elit," + "sed do eiusmod tempor incididunt ut labore et dolore", + "magna aliqua." + ] + ``` + */ + description?: string | string[]; + /** + * http Object or Function For input arguments: a function or an object describing mapping from HTTP request to the argument value. See HTTP mapping of input arguments below. + * http.target + * Map the callback argument value to the HTTP response object. The following values are supported. + + * * status sets the res.statusCode to the provided value + * * header sets the http.header or arg named header to the value + */ + http?: l.RemoteHttpOptions; + + /** + * True if argument is required; false otherwise. + */ + required?: boolean; + + /** + * For callback arguments: set this property to true if your function has a single callback argument to use as the root object returned to remote caller. Otherwise the root object returned is a map (argument-name to argument-value). + */ + root?: boolean; + + /** + * Argument datatype; must be a Loopback type. Additionally, callback arguments allow a special type "file"; see below. + */ + type: "any" | "Array" | "Boolean" | "Buffer" | "Date" | "GeoPoint" | "null" | "Number" | "Object" | "String"; + + /** + * Default value that will be used to populate loopback-explorer input fields and swagger documentation. Note: This value will not be passed into remote methods function if argument is not present. + */ + default?: string; + } + + /** * The base class for **all models** * **Inheriting from `Model`* @@ -880,7 +1040,7 @@ declare namespace l { * @param {any} options The remoting options. * See [Remote methods - Options](docs.strongloop.com/display/LB/Remote+methods#Remotemethods-Options) */ - remoteMethod(name: string, options: any): void; + static remoteMethod(name: string, options: RemoteMethodOptions): void; /** * The `loopback.Model.extend()` method calls this when you create a model that extends another model.