lumen-framework 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/README.md +164 -0
- package/bin/lumen +275 -0
- package/dist/cli.cjs +3002 -0
- package/dist/cli.cjs.map +7 -0
- package/dist/index.js +6515 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +6480 -0
- package/dist/index.mjs.map +7 -0
- package/dist/types/constants.d.ts +7 -0
- package/dist/types/errors/controller-missing-error.d.ts +7 -0
- package/dist/types/errors/serializer-missing-error.d.ts +7 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/interfaces.d.ts +12 -0
- package/dist/types/packages/application/index.d.ts +97 -0
- package/dist/types/packages/application/initialize.d.ts +6 -0
- package/dist/types/packages/application/interfaces.d.ts +16 -0
- package/dist/types/packages/application/utils/create-controller.d.ts +12 -0
- package/dist/types/packages/application/utils/create-serializer.d.ts +4 -0
- package/dist/types/packages/application/utils/normalize-port.d.ts +4 -0
- package/dist/types/packages/cli/commands/build.d.ts +1 -0
- package/dist/types/packages/cli/commands/create.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbcreate.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbdrop.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbmigrate.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbrollback.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbseed.d.ts +4 -0
- package/dist/types/packages/cli/commands/destroy.d.ts +11 -0
- package/dist/types/packages/cli/commands/generate.d.ts +10 -0
- package/dist/types/packages/cli/commands/index.d.ts +12 -0
- package/dist/types/packages/cli/commands/repl.d.ts +1 -0
- package/dist/types/packages/cli/commands/serve.d.ts +8 -0
- package/dist/types/packages/cli/commands/test.d.ts +4 -0
- package/dist/types/packages/cli/constants.d.ts +2 -0
- package/dist/types/packages/cli/errors/database-config-missing.d.ts +4 -0
- package/dist/types/packages/cli/generator/constants.d.ts +5 -0
- package/dist/types/packages/cli/generator/index.d.ts +11 -0
- package/dist/types/packages/cli/generator/interfaces.d.ts +9 -0
- package/dist/types/packages/cli/generator/utils/create-generator.d.ts +9 -0
- package/dist/types/packages/cli/generator/utils/generate-type.d.ts +33 -0
- package/dist/types/packages/cli/generator/utils/generator-for.d.ts +2 -0
- package/dist/types/packages/cli/generator/utils/log.d.ts +4 -0
- package/dist/types/packages/cli/generator/utils/migration-conflict.d.ts +6 -0
- package/dist/types/packages/cli/templates/application.d.ts +5 -0
- package/dist/types/packages/cli/templates/config.d.ts +5 -0
- package/dist/types/packages/cli/templates/controller.d.ts +5 -0
- package/dist/types/packages/cli/templates/database.d.ts +5 -0
- package/dist/types/packages/cli/templates/empty-migration.d.ts +5 -0
- package/dist/types/packages/cli/templates/eslint-config.d.ts +5 -0
- package/dist/types/packages/cli/templates/gitignore.d.ts +5 -0
- package/dist/types/packages/cli/templates/license.d.ts +5 -0
- package/dist/types/packages/cli/templates/middleware.d.ts +5 -0
- package/dist/types/packages/cli/templates/model-migration.d.ts +5 -0
- package/dist/types/packages/cli/templates/model.d.ts +5 -0
- package/dist/types/packages/cli/templates/package-json.d.ts +5 -0
- package/dist/types/packages/cli/templates/readme.d.ts +5 -0
- package/dist/types/packages/cli/templates/routes.d.ts +5 -0
- package/dist/types/packages/cli/templates/seed.d.ts +5 -0
- package/dist/types/packages/cli/templates/serializer.d.ts +5 -0
- package/dist/types/packages/cli/templates/util.d.ts +5 -0
- package/dist/types/packages/cli/utils/create-prompt.d.ts +4 -0
- package/dist/types/packages/cli/utils/driver-for.d.ts +1 -0
- package/dist/types/packages/cli/utils/indent.d.ts +1 -0
- package/dist/types/packages/compiler/index.d.ts +11 -0
- package/dist/types/packages/compiler/interfaces.d.ts +1 -0
- package/dist/types/packages/compiler/utils/create-boot-script.d.ts +6 -0
- package/dist/types/packages/compiler/utils/create-manifest.d.ts +6 -0
- package/dist/types/packages/compiler/utils/format-name.d.ts +5 -0
- package/dist/types/packages/config/index.d.ts +3 -0
- package/dist/types/packages/config/interfaces.d.ts +6 -0
- package/dist/types/packages/controller/constants.d.ts +2 -0
- package/dist/types/packages/controller/index.d.ts +591 -0
- package/dist/types/packages/controller/interfaces.d.ts +13 -0
- package/dist/types/packages/controller/utils/find-many.d.ts +6 -0
- package/dist/types/packages/controller/utils/find-one.d.ts +6 -0
- package/dist/types/packages/controller/utils/params-to-query.d.ts +16 -0
- package/dist/types/packages/controller/utils/resolve-relationships.d.ts +5 -0
- package/dist/types/packages/database/attribute/index.d.ts +6 -0
- package/dist/types/packages/database/attribute/interfaces.d.ts +6 -0
- package/dist/types/packages/database/attribute/utils/create-attribute.d.ts +5 -0
- package/dist/types/packages/database/attribute/utils/create-getter.d.ts +3 -0
- package/dist/types/packages/database/attribute/utils/create-normalizer.d.ts +1 -0
- package/dist/types/packages/database/attribute/utils/create-setter.d.ts +8 -0
- package/dist/types/packages/database/change-set/index.d.ts +10 -0
- package/dist/types/packages/database/constants.d.ts +3 -0
- package/dist/types/packages/database/errors/config-missing-error.d.ts +4 -0
- package/dist/types/packages/database/errors/index.d.ts +5 -0
- package/dist/types/packages/database/errors/invalid-driver-error.d.ts +7 -0
- package/dist/types/packages/database/errors/migrations-pending-error.d.ts +7 -0
- package/dist/types/packages/database/errors/model-missing-error.d.ts +7 -0
- package/dist/types/packages/database/errors/unique-constraint-error.d.ts +4 -0
- package/dist/types/packages/database/index.d.ts +26 -0
- package/dist/types/packages/database/initialize.d.ts +6 -0
- package/dist/types/packages/database/interfaces.d.ts +85 -0
- package/dist/types/packages/database/migration/index.d.ts +12 -0
- package/dist/types/packages/database/migration/interfaces.d.ts +4 -0
- package/dist/types/packages/database/migration/utils/generate-timestamp.d.ts +2 -0
- package/dist/types/packages/database/model/index.d.ts +1046 -0
- package/dist/types/packages/database/model/initialize-class.d.ts +10 -0
- package/dist/types/packages/database/model/interfaces.d.ts +14 -0
- package/dist/types/packages/database/model/utils/get-columns.d.ts +5 -0
- package/dist/types/packages/database/model/utils/persistence.d.ts +18 -0
- package/dist/types/packages/database/model/utils/process-write-error.d.ts +4 -0
- package/dist/types/packages/database/model/utils/run-hooks.d.ts +6 -0
- package/dist/types/packages/database/model/utils/validate.d.ts +5 -0
- package/dist/types/packages/database/query/errors/index.d.ts +1 -0
- package/dist/types/packages/database/query/errors/record-not-found-error.d.ts +6 -0
- package/dist/types/packages/database/query/index.d.ts +38 -0
- package/dist/types/packages/database/query/runner/constants.d.ts +2 -0
- package/dist/types/packages/database/query/runner/index.d.ts +9 -0
- package/dist/types/packages/database/query/runner/utils/build-results.d.ts +10 -0
- package/dist/types/packages/database/query/runner/utils/get-find-param.d.ts +2 -0
- package/dist/types/packages/database/query/utils/format-select.d.ts +5 -0
- package/dist/types/packages/database/query/utils/scopes-for.d.ts +2 -0
- package/dist/types/packages/database/relationship/index.d.ts +11 -0
- package/dist/types/packages/database/relationship/interfaces.d.ts +12 -0
- package/dist/types/packages/database/relationship/utils/getters.d.ts +14 -0
- package/dist/types/packages/database/relationship/utils/inverse-setters.d.ts +15 -0
- package/dist/types/packages/database/relationship/utils/setters.d.ts +14 -0
- package/dist/types/packages/database/relationship/utils/unassociate.d.ts +5 -0
- package/dist/types/packages/database/relationship/utils/update-relationship.d.ts +5 -0
- package/dist/types/packages/database/relationship/utils/validate-type.d.ts +2 -0
- package/dist/types/packages/database/transaction/index.d.ts +16 -0
- package/dist/types/packages/database/transaction/interfaces.d.ts +5 -0
- package/dist/types/packages/database/utils/connect.d.ts +6 -0
- package/dist/types/packages/database/utils/create-migrations.d.ts +5 -0
- package/dist/types/packages/database/utils/normalize-model-name.d.ts +5 -0
- package/dist/types/packages/database/utils/pending-migrations.d.ts +4 -0
- package/dist/types/packages/database/utils/type-for-column.d.ts +5 -0
- package/dist/types/packages/database/validation/errors/index.d.ts +1 -0
- package/dist/types/packages/database/validation/errors/validation-error.d.ts +7 -0
- package/dist/types/packages/database/validation/index.d.ts +13 -0
- package/dist/types/packages/database/validation/interfaces.d.ts +5 -0
- package/dist/types/packages/freezeable/constants.d.ts +1 -0
- package/dist/types/packages/freezeable/index.d.ts +3 -0
- package/dist/types/packages/freezeable/map/index.d.ts +11 -0
- package/dist/types/packages/freezeable/set/index.d.ts +11 -0
- package/dist/types/packages/freezeable/utils/freeze.d.ts +20 -0
- package/dist/types/packages/freezeable/utils/is-frozen.d.ts +4 -0
- package/dist/types/packages/fs/index.d.ts +52 -0
- package/dist/types/packages/fs/interfaces.d.ts +19 -0
- package/dist/types/packages/fs/utils/create-path-remover.d.ts +5 -0
- package/dist/types/packages/fs/utils/create-resolver.d.ts +4 -0
- package/dist/types/packages/fs/utils/exists.d.ts +4 -0
- package/dist/types/packages/fs/utils/is-js-file.d.ts +4 -0
- package/dist/types/packages/fs/utils/parse-path.d.ts +5 -0
- package/dist/types/packages/fs/utils/rmrf.d.ts +5 -0
- package/dist/types/packages/fs/watcher/index.d.ts +13 -0
- package/dist/types/packages/fs/watcher/initialize.d.ts +5 -0
- package/dist/types/packages/fs/watcher/interfaces.d.ts +3 -0
- package/dist/types/packages/jsonapi/constants.d.ts +2 -0
- package/dist/types/packages/jsonapi/errors/index.d.ts +3 -0
- package/dist/types/packages/jsonapi/errors/invalid-content-type-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/errors/not-acceptable-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/errors/unsupported-media-type-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/index.d.ts +5 -0
- package/dist/types/packages/jsonapi/interfaces.d.ts +66 -0
- package/dist/types/packages/jsonapi/utils/has-media-type.d.ts +4 -0
- package/dist/types/packages/jsonapi/utils/is-jsonapi.d.ts +4 -0
- package/dist/types/packages/loader/builder/index.d.ts +6 -0
- package/dist/types/packages/loader/builder/interfaces.d.ts +10 -0
- package/dist/types/packages/loader/builder/utils/create-children-builder.d.ts +2 -0
- package/dist/types/packages/loader/builder/utils/create-parent-builder.d.ts +2 -0
- package/dist/types/packages/loader/builder/utils/sort-by-namespace.d.ts +5 -0
- package/dist/types/packages/loader/index.d.ts +8 -0
- package/dist/types/packages/loader/interfaces.d.ts +4 -0
- package/dist/types/packages/loader/resolver/index.d.ts +7 -0
- package/dist/types/packages/loader/resolver/utils/closest-ancestor.d.ts +2 -0
- package/dist/types/packages/loader/resolver/utils/closest-child.d.ts +2 -0
- package/dist/types/packages/loader/utils/bundle-for.d.ts +5 -0
- package/dist/types/packages/loader/utils/format-key.d.ts +4 -0
- package/dist/types/packages/logger/constants.d.ts +8 -0
- package/dist/types/packages/logger/index.d.ts +182 -0
- package/dist/types/packages/logger/interfaces.d.ts +17 -0
- package/dist/types/packages/logger/request-logger/index.d.ts +6 -0
- package/dist/types/packages/logger/request-logger/interfaces.d.ts +25 -0
- package/dist/types/packages/logger/request-logger/templates.d.ts +9 -0
- package/dist/types/packages/logger/request-logger/utils/filter-params.d.ts +4 -0
- package/dist/types/packages/logger/request-logger/utils/log-json.d.ts +10 -0
- package/dist/types/packages/logger/request-logger/utils/log-text.d.ts +10 -0
- package/dist/types/packages/logger/utils/line.d.ts +4 -0
- package/dist/types/packages/logger/utils/sql.d.ts +4 -0
- package/dist/types/packages/logger/writer/constants.d.ts +3 -0
- package/dist/types/packages/logger/writer/index.d.ts +6 -0
- package/dist/types/packages/logger/writer/interfaces.d.ts +2 -0
- package/dist/types/packages/logger/writer/utils/format-message.d.ts +9 -0
- package/dist/types/packages/lumenify/index.d.ts +11 -0
- package/dist/types/packages/lumenify/utils/create-response-proxy.d.ts +8 -0
- package/dist/types/packages/pm/cluster/index.d.ts +21 -0
- package/dist/types/packages/pm/cluster/interfaces.d.ts +7 -0
- package/dist/types/packages/pm/index.d.ts +6 -0
- package/dist/types/packages/router/definitions/context/index.d.ts +11 -0
- package/dist/types/packages/router/definitions/context/utils/create-definition-group.d.ts +7 -0
- package/dist/types/packages/router/definitions/context/utils/create-definition.d.ts +10 -0
- package/dist/types/packages/router/definitions/context/utils/normalize-resource-args.d.ts +12 -0
- package/dist/types/packages/router/definitions/index.d.ts +10 -0
- package/dist/types/packages/router/definitions/interfaces.d.ts +7 -0
- package/dist/types/packages/router/index.d.ts +18 -0
- package/dist/types/packages/router/interfaces.d.ts +18 -0
- package/dist/types/packages/router/namespace/index.d.ts +20 -0
- package/dist/types/packages/router/namespace/interfaces.d.ts +9 -0
- package/dist/types/packages/router/namespace/utils/normalize-name.d.ts +4 -0
- package/dist/types/packages/router/namespace/utils/normalize-path.d.ts +4 -0
- package/dist/types/packages/router/resource/index.d.ts +13 -0
- package/dist/types/packages/router/resource/interfaces.d.ts +5 -0
- package/dist/types/packages/router/resource/utils/normalize-only.d.ts +5 -0
- package/dist/types/packages/router/route/action/constants.d.ts +1 -0
- package/dist/types/packages/router/route/action/enhancers/resource.d.ts +5 -0
- package/dist/types/packages/router/route/action/enhancers/track-perf.d.ts +5 -0
- package/dist/types/packages/router/route/action/index.d.ts +9 -0
- package/dist/types/packages/router/route/action/interfaces.d.ts +2 -0
- package/dist/types/packages/router/route/action/utils/create-page-links.d.ts +12 -0
- package/dist/types/packages/router/route/action/utils/get-action-name.d.ts +5 -0
- package/dist/types/packages/router/route/action/utils/get-controller-name.d.ts +5 -0
- package/dist/types/packages/router/route/constants.d.ts +1 -0
- package/dist/types/packages/router/route/index.d.ts +28 -0
- package/dist/types/packages/router/route/interfaces.d.ts +10 -0
- package/dist/types/packages/router/route/params/errors/index.d.ts +5 -0
- package/dist/types/packages/router/route/params/errors/invalid-parameter-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/errors/parameter-not-nullable-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/parameter-required-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/errors/parameter-type-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/parameter-value-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/resource-mismatch-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/index.d.ts +18 -0
- package/dist/types/packages/router/route/params/interfaces.d.ts +25 -0
- package/dist/types/packages/router/route/params/parameter/index.d.ts +14 -0
- package/dist/types/packages/router/route/params/parameter/interfaces.d.ts +4 -0
- package/dist/types/packages/router/route/params/parameter/utils/validate-value.d.ts +5 -0
- package/dist/types/packages/router/route/params/parameter-group/index.d.ts +14 -0
- package/dist/types/packages/router/route/params/parameter-group/utils/has-required-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/get-data-params.d.ts +6 -0
- package/dist/types/packages/router/route/params/utils/get-default-collection-params.d.ts +6 -0
- package/dist/types/packages/router/route/params/utils/get-default-member-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/get-query-params.d.ts +14 -0
- package/dist/types/packages/router/route/params/utils/get-url-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/validate-resource-id.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/validate-type.d.ts +5 -0
- package/dist/types/packages/router/route/utils/get-dynamic-segments.d.ts +4 -0
- package/dist/types/packages/router/route/utils/get-static-path.d.ts +4 -0
- package/dist/types/packages/router/utils/create-replacer.d.ts +5 -0
- package/dist/types/packages/serializer/index.d.ts +521 -0
- package/dist/types/packages/serializer/interfaces.d.ts +7 -0
- package/dist/types/packages/server/constants.d.ts +2 -0
- package/dist/types/packages/server/index.d.ts +27 -0
- package/dist/types/packages/server/interfaces.d.ts +18 -0
- package/dist/types/packages/server/request/constants.d.ts +2 -0
- package/dist/types/packages/server/request/index.d.ts +8 -0
- package/dist/types/packages/server/request/interfaces.d.ts +63 -0
- package/dist/types/packages/server/request/parser/constants.d.ts +6 -0
- package/dist/types/packages/server/request/parser/errors/index.d.ts +1 -0
- package/dist/types/packages/server/request/parser/errors/malformed-request-error.d.ts +8 -0
- package/dist/types/packages/server/request/parser/index.d.ts +5 -0
- package/dist/types/packages/server/request/parser/utils/format.d.ts +17 -0
- package/dist/types/packages/server/request/parser/utils/parse-nested-object.d.ts +4 -0
- package/dist/types/packages/server/request/parser/utils/parse-read.d.ts +5 -0
- package/dist/types/packages/server/request/parser/utils/parse-write.d.ts +5 -0
- package/dist/types/packages/server/request/utils/get-domain.d.ts +5 -0
- package/dist/types/packages/server/responder/index.d.ts +5 -0
- package/dist/types/packages/server/responder/utils/data-for.d.ts +5 -0
- package/dist/types/packages/server/responder/utils/has-content-type.d.ts +2 -0
- package/dist/types/packages/server/responder/utils/normalize.d.ts +7 -0
- package/dist/types/packages/server/response/index.d.ts +5 -0
- package/dist/types/packages/server/response/interfaces.d.ts +22 -0
- package/dist/types/packages/server/utils/create-server-error.d.ts +7 -0
- package/dist/types/packages/server/utils/set-cors-headers.d.ts +3 -0
- package/dist/types/packages/server/utils/validate-accept.d.ts +4 -0
- package/dist/types/packages/server/utils/validate-content-type.d.ts +4 -0
- package/dist/types/packages/template/index.d.ts +8 -0
- package/dist/types/packages/template/utils/insert-values.d.ts +4 -0
- package/dist/types/utils/chain.d.ts +5 -0
- package/dist/types/utils/compact.d.ts +11 -0
- package/dist/types/utils/compose.d.ts +19 -0
- package/dist/types/utils/create-query-string.d.ts +6 -0
- package/dist/types/utils/diff.d.ts +4 -0
- package/dist/types/utils/entries.d.ts +4 -0
- package/dist/types/utils/env.d.ts +3 -0
- package/dist/types/utils/exec.d.ts +5 -0
- package/dist/types/utils/github.d.ts +9 -0
- package/dist/types/utils/has-own-property.d.ts +1 -0
- package/dist/types/utils/insert.d.ts +4 -0
- package/dist/types/utils/is-buffer.d.ts +4 -0
- package/dist/types/utils/is-null.d.ts +4 -0
- package/dist/types/utils/is-object.d.ts +17 -0
- package/dist/types/utils/is-undefined.d.ts +4 -0
- package/dist/types/utils/k.d.ts +10 -0
- package/dist/types/utils/map-to-object.d.ts +1 -0
- package/dist/types/utils/merge.d.ts +4 -0
- package/dist/types/utils/omit.d.ts +9 -0
- package/dist/types/utils/pick.d.ts +11 -0
- package/dist/types/utils/present.d.ts +4 -0
- package/dist/types/utils/promise-hash.d.ts +9 -0
- package/dist/types/utils/proxy.d.ts +6 -0
- package/dist/types/utils/range.d.ts +4 -0
- package/dist/types/utils/set-type.d.ts +11 -0
- package/dist/types/utils/sleep.d.ts +4 -0
- package/dist/types/utils/stringify.d.ts +4 -0
- package/dist/types/utils/transform-keys.d.ts +29 -0
- package/dist/types/utils/try-catch.d.ts +55 -0
- package/dist/types/utils/underscore.d.ts +4 -0
- package/dist/types/utils/uniq.d.ts +4 -0
- package/package.json +87 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
import type Serializer from '../serializer';
|
|
2
|
+
import type { Model, ModelClass, Query } from '../database';
|
|
3
|
+
import type { Request, Response } from '../server';
|
|
4
|
+
import type { Controller$opts, Controller$beforeAction, Controller$afterAction } from './interfaces';
|
|
5
|
+
/**
|
|
6
|
+
* ## Overview
|
|
7
|
+
*
|
|
8
|
+
* The Controller class is responsible for taking in requests from the outside
|
|
9
|
+
* world and returning the appropriate response.
|
|
10
|
+
*
|
|
11
|
+
* Think of a Controller as a server at a restaurant. A client makes a request
|
|
12
|
+
* to an application, that request is routed to the appropriate Controller and
|
|
13
|
+
* then the Controller interprets the request and returns data relative to what
|
|
14
|
+
* the client has request.
|
|
15
|
+
*
|
|
16
|
+
* #### Actions
|
|
17
|
+
*
|
|
18
|
+
* Controller actions are functions that call on a Controller in response to an
|
|
19
|
+
* incoming HTTP request. The job of Controller actions are to return the data
|
|
20
|
+
* that the Lumen Application will respond with.
|
|
21
|
+
*
|
|
22
|
+
* There is no special API for Controller actions. They are simply functions
|
|
23
|
+
* that return a value. If an action returns a Query or Promise the resolved
|
|
24
|
+
* value will be used rather than the immediate return value of the action.
|
|
25
|
+
*
|
|
26
|
+
* Below you will find a table showing the different types of responses you can
|
|
27
|
+
* get from different action return values. Keep in mind, Lumen is agnostic to
|
|
28
|
+
* whether or not the value is returned synchronously or resolved from a
|
|
29
|
+
* Promise.
|
|
30
|
+
*
|
|
31
|
+
* | Return/Resolved Value | Response |
|
|
32
|
+
* |------------------------------|--------------------------------------------|
|
|
33
|
+
* | Array<Model> or Model | Serialized JSON String |
|
|
34
|
+
* | Array or Object Literal | JSON String |
|
|
35
|
+
* | String Literal | Plain Text |
|
|
36
|
+
* | Number Literal | [HTTP Status Code](https://goo.gl/T2lMc7) |
|
|
37
|
+
* | true | [204 No Content](https://goo.gl/GxKoqz) |
|
|
38
|
+
* | false | [401 Unauthorized](https://goo.gl/60QqCW) |
|
|
39
|
+
*
|
|
40
|
+
* **Built-In Actions**
|
|
41
|
+
*
|
|
42
|
+
* Built-in actions refer to Controller actions that you get for free when
|
|
43
|
+
* extending the Controller class (show, index, create, update, destroy). These
|
|
44
|
+
* actions are highly optimized to load only the attributes and relationships
|
|
45
|
+
* that are defined in the resolved Serializer for a Controller.
|
|
46
|
+
*
|
|
47
|
+
* If applicable, built-in actions support the following features described in
|
|
48
|
+
* the [JSON API specification](http://jsonapi.org/):
|
|
49
|
+
*
|
|
50
|
+
* - [Sorting](http://jsonapi.org/format/#fetching-sorting)
|
|
51
|
+
* - [Filtering](http://jsonapi.org/format/#fetching-filtering)
|
|
52
|
+
* - [Pagination](http://jsonapi.org/format/#fetching-pagination)
|
|
53
|
+
* - [Sparse Fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets)
|
|
54
|
+
* - [Including Related Resources](http://jsonapi.org/format/#fetching-includes)
|
|
55
|
+
*
|
|
56
|
+
* **Extending Built-In Actions**
|
|
57
|
+
*
|
|
58
|
+
* Considering the amount of functionality built-in actions provide, you will
|
|
59
|
+
* rarely need to override the default behavior of a built-in action. In the
|
|
60
|
+
* event that you do need to override a built-in action, you have the ability to
|
|
61
|
+
* opt back into the built-in logic by calling the super class.
|
|
62
|
+
*
|
|
63
|
+
* Read actions such as index and show return a Query which allows us to chain
|
|
64
|
+
* methods to the super call. In the following example we will extend the
|
|
65
|
+
* default behavior of the index action to only match records that meet an
|
|
66
|
+
* additional hard-coded set of conditions. We will still be able to use all of
|
|
67
|
+
* the functionality that the built-in index action provides.
|
|
68
|
+
*
|
|
69
|
+
* ```javascript
|
|
70
|
+
* // app/controllers/posts.js
|
|
71
|
+
* import { Controller } from 'lumen-framework';
|
|
72
|
+
*
|
|
73
|
+
* class PostsController extends Controller {
|
|
74
|
+
* index(request, response) {
|
|
75
|
+
* return super.index(request, response).where({
|
|
76
|
+
* isPublic: true
|
|
77
|
+
* });
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* export default PostsController;
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* **Custom Actions**
|
|
85
|
+
*
|
|
86
|
+
* Sometimes it is necessary to add a custom action to a Controller. Lumen allows
|
|
87
|
+
* you to do so by adding an instance method to a Controller. In the following
|
|
88
|
+
* example you will see how to add a custom action with the name `check` to a
|
|
89
|
+
* Controller. We are implementing this action to use as a health check for the
|
|
90
|
+
* application so we want to return the `Number` literal `204`.
|
|
91
|
+
*
|
|
92
|
+
* ```javascript
|
|
93
|
+
* // app/controllers/health.js
|
|
94
|
+
* import { Controller } from 'lumen-framework';
|
|
95
|
+
*
|
|
96
|
+
* class HealthController extends Controller {
|
|
97
|
+
* async check() {
|
|
98
|
+
* return 204;
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
101
|
+
*
|
|
102
|
+
* export default HealthController;
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* The example above is nice but we can make the code a bit more concise with an
|
|
106
|
+
* Arrow `Function`.
|
|
107
|
+
*
|
|
108
|
+
* ```javascript
|
|
109
|
+
* // app/controllers/health.js
|
|
110
|
+
* import { Controller } from 'lumen-framework';
|
|
111
|
+
*
|
|
112
|
+
* class HealthController extends Controller {
|
|
113
|
+
* check = async () => 204;
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* export default HealthController;
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* Using an Arrow Function instead of a traditional method Controller can be
|
|
120
|
+
* useful when immediately returning a value. However, there are a few downsides
|
|
121
|
+
* to using an Arrow `Function` for a Controller action, such as not being able
|
|
122
|
+
* to call the `super class`. This can be an issue if you are looking to extend
|
|
123
|
+
* a built-in action.
|
|
124
|
+
*
|
|
125
|
+
* Another use case for a custom action could be to return a specific scope of
|
|
126
|
+
* data from a `Model`. Let's implement
|
|
127
|
+
* a custom `drafts` route on a `PostsController`.
|
|
128
|
+
*
|
|
129
|
+
* ```javascript
|
|
130
|
+
* // app/controllers/posts.js
|
|
131
|
+
* import { Controller } from 'lumen-framework';
|
|
132
|
+
* import Post from 'app/models/posts';
|
|
133
|
+
*
|
|
134
|
+
* class PostsController extends Controller {
|
|
135
|
+
* drafts() {
|
|
136
|
+
* return Post.where({
|
|
137
|
+
* isPublic: false
|
|
138
|
+
* });
|
|
139
|
+
* }
|
|
140
|
+
* }
|
|
141
|
+
*
|
|
142
|
+
* export default PostsController;
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* While the example above works, we would have to implement all the custom
|
|
146
|
+
* logic that we get for free with built-in actions. Since we aren't getting too
|
|
147
|
+
* crazy with our custom action we can likely just call the `index` action and
|
|
148
|
+
* chain a `.where()` to it.
|
|
149
|
+
*
|
|
150
|
+
* ```javascript
|
|
151
|
+
* // app/controllers/posts.js
|
|
152
|
+
* import { Controller } from 'lumen-framework';
|
|
153
|
+
*
|
|
154
|
+
* class PostsController extends Controller {
|
|
155
|
+
* drafts(request, response) {
|
|
156
|
+
* return this.index(request, response).where({
|
|
157
|
+
* isPublic: false
|
|
158
|
+
* });
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
*
|
|
162
|
+
* export default PostsController;
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* Now we can sort, filter, and paginate our custom `drafts` route!
|
|
166
|
+
*
|
|
167
|
+
* #### Middleware
|
|
168
|
+
*
|
|
169
|
+
* Middleware can be a very powerful tool in many Node.js server frameworks. Lumen
|
|
170
|
+
* is no exception. Middleware can be used to execute logic before or after a
|
|
171
|
+
* Controller action is executed.
|
|
172
|
+
*
|
|
173
|
+
* There are two hooks where you can execute middleware functions,
|
|
174
|
+
* `beforeAction` and `afterAction`. Functions added to the `beforeAction` hook
|
|
175
|
+
* will execute before the Controller action and functions added to the
|
|
176
|
+
* `afterAction` hook will be executed after the `Controller` action.
|
|
177
|
+
*
|
|
178
|
+
* **Context**
|
|
179
|
+
*
|
|
180
|
+
* Middleware functions will be bound to the Controller they are added to upon
|
|
181
|
+
* the start of an Application.
|
|
182
|
+
*
|
|
183
|
+
* Due to the lexical binding of arrow functions, if you need to use the `this`
|
|
184
|
+
* keyword within a middleware function, declare the middleware function using
|
|
185
|
+
* the `function` keyword and not as an arrow function.
|
|
186
|
+
*
|
|
187
|
+
* **Scoping Middleware**
|
|
188
|
+
*
|
|
189
|
+
* Middleware is scoped by Controller and includes a parent Controller's
|
|
190
|
+
* middleware recursively until the parent Controller is the root
|
|
191
|
+
* `ApplicationController`. This allows you to implement custom logic that can
|
|
192
|
+
* be executed for resources, namespaces, or an entire Application.
|
|
193
|
+
*
|
|
194
|
+
* Let's say we want to require authentication for every route in our
|
|
195
|
+
* Application. All we have to do is move our authentication middleware function
|
|
196
|
+
* from the example above to the `ApplicationController`.
|
|
197
|
+
*
|
|
198
|
+
* ```javascript
|
|
199
|
+
* // app/controllers/application.js
|
|
200
|
+
* import { Controller } from 'lumen-framework';
|
|
201
|
+
*
|
|
202
|
+
* class ApplicationController extends Controller {
|
|
203
|
+
* beforeAction = [
|
|
204
|
+
* async function authenticate(request) {
|
|
205
|
+
* if (!request.currentUser) {
|
|
206
|
+
* // 401 Unauthorized
|
|
207
|
+
* return false;
|
|
208
|
+
* }
|
|
209
|
+
* }
|
|
210
|
+
* ];
|
|
211
|
+
* }
|
|
212
|
+
*
|
|
213
|
+
* export default ApplicationController;
|
|
214
|
+
* ```
|
|
215
|
+
*
|
|
216
|
+
* **Execuation Order**
|
|
217
|
+
*
|
|
218
|
+
* Understanding the execution order of middleware functions and a `Controller`
|
|
219
|
+
* action is essential to productivity with Lumen. Depending on what you use case
|
|
220
|
+
* is, you may want your function to execute at different times in the
|
|
221
|
+
* `request` / `response` cycle.
|
|
222
|
+
*
|
|
223
|
+
* 1. Parent `Controller` `beforeAction` hooks
|
|
224
|
+
* 2. `Controller` `beforeAction` hooks
|
|
225
|
+
* 3. `Controller` Action
|
|
226
|
+
* 4. `Controller` `afterAction` hooks
|
|
227
|
+
* 5. Parent `Controller` `afterAction` hooks
|
|
228
|
+
*
|
|
229
|
+
* **Modules**
|
|
230
|
+
*
|
|
231
|
+
* It is considered a best practice to define your middleware functions in
|
|
232
|
+
* separate file and export them for use throughout an Application. Typically
|
|
233
|
+
* this is done within an `app/middleware` directory.
|
|
234
|
+
*
|
|
235
|
+
* ```javascript
|
|
236
|
+
* // app/middleware/authenticate.js
|
|
237
|
+
* export default async function authenticate(request) {
|
|
238
|
+
* if (!request.currentUser) {
|
|
239
|
+
* // 401 Unauthorized
|
|
240
|
+
* return false;
|
|
241
|
+
* }
|
|
242
|
+
* }
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* This keeps the Controller code clean, easier to read, and easier to modify.
|
|
246
|
+
*
|
|
247
|
+
* ```javascript
|
|
248
|
+
* // app/controllers/application.js
|
|
249
|
+
* import { Controller } from 'lumen-framework';
|
|
250
|
+
* import authenticate from 'app/middleware/authenticate';
|
|
251
|
+
*
|
|
252
|
+
* class ApplicationController extends Controller {
|
|
253
|
+
* beforeAction = [
|
|
254
|
+
* authenticate
|
|
255
|
+
* ];
|
|
256
|
+
* }
|
|
257
|
+
*
|
|
258
|
+
* export default ApplicationController;
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* @class Controller
|
|
262
|
+
* @public
|
|
263
|
+
*/
|
|
264
|
+
declare class Controller {
|
|
265
|
+
/**
|
|
266
|
+
* An array of custom query parameter keys that are allowed to reach a
|
|
267
|
+
* Controller instance from an incoming `HTTP` request.
|
|
268
|
+
*
|
|
269
|
+
* For security reasons, query parameters passed to Controller actions from an
|
|
270
|
+
* incoming request other than sort, filter, and page must have their key
|
|
271
|
+
* whitelisted.
|
|
272
|
+
*
|
|
273
|
+
* ```javascript
|
|
274
|
+
* class UsersController extends Controller {
|
|
275
|
+
* // Allow the following custom query parameters to be used for this
|
|
276
|
+
* // Controller's actions.
|
|
277
|
+
* query = [
|
|
278
|
+
* 'cache'
|
|
279
|
+
* ];
|
|
280
|
+
* }
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* @property query
|
|
284
|
+
* @type {Array}
|
|
285
|
+
* @default []
|
|
286
|
+
* @public
|
|
287
|
+
*/
|
|
288
|
+
query: Array<string>;
|
|
289
|
+
/**
|
|
290
|
+
* An array of sort query parameter values that are allowed to reach a
|
|
291
|
+
* Controller instance from an incoming `HTTP` request.
|
|
292
|
+
*
|
|
293
|
+
* If you do not override this property all of the attributes specified in the
|
|
294
|
+
* Serializer that represents a Controller's resource. If the Serializer
|
|
295
|
+
* cannot be resolved, this property will default to an empty array.
|
|
296
|
+
*
|
|
297
|
+
* @property sort
|
|
298
|
+
* @type {Array}
|
|
299
|
+
* @default []
|
|
300
|
+
* @public
|
|
301
|
+
*/
|
|
302
|
+
sort: Array<string>;
|
|
303
|
+
/**
|
|
304
|
+
* An array of filter query parameter keys that are allowed to reach a
|
|
305
|
+
* Controller instance from an incoming `HTTP` request.
|
|
306
|
+
*
|
|
307
|
+
* If you do not override this property all of the attributes specified in the
|
|
308
|
+
* Serializer that represents a Controller's resource. If the Serializer
|
|
309
|
+
* cannot be resolved, this property will default to an empty array.
|
|
310
|
+
*
|
|
311
|
+
* @property filter
|
|
312
|
+
* @type {Array}
|
|
313
|
+
* @default []
|
|
314
|
+
* @public
|
|
315
|
+
*/
|
|
316
|
+
filter: Array<string>;
|
|
317
|
+
/**
|
|
318
|
+
* An array of parameter keys that are allowed to reach a Controller instance
|
|
319
|
+
* from an incoming `POST` or `PATCH` request body.
|
|
320
|
+
*
|
|
321
|
+
* If you do not override this property all of the attributes specified in the
|
|
322
|
+
* Serializer that represents a Controller's resource. If the Serializer
|
|
323
|
+
* cannot be resolved, this property will default to an empty array.
|
|
324
|
+
*
|
|
325
|
+
* @property params
|
|
326
|
+
* @type {Array}
|
|
327
|
+
* @default []
|
|
328
|
+
* @public
|
|
329
|
+
*/
|
|
330
|
+
params: Array<string>;
|
|
331
|
+
/**
|
|
332
|
+
* Functions to execute on each request handled by a `Controller` before the
|
|
333
|
+
* `Controller` action is executed.
|
|
334
|
+
*
|
|
335
|
+
* Functions added to the `beforeAction` hook behave similarly to `Controller`
|
|
336
|
+
* actions, however, they are expected to return `undefined`. If a middleware
|
|
337
|
+
* function returns a value other than `undefined` the `request` / `response`
|
|
338
|
+
* cycle will end before remaining middleware and/or Controller actions are
|
|
339
|
+
* executed. This makes the `beforeAction` hook a very powerful tool for
|
|
340
|
+
* dealing with many common tasks, such as authentication.
|
|
341
|
+
*
|
|
342
|
+
* Functions called from the `beforeAction` hook will have `request` and
|
|
343
|
+
* `response` objects passed as arguments.
|
|
344
|
+
*
|
|
345
|
+
* **Example:**
|
|
346
|
+
*
|
|
347
|
+
* ```javascript
|
|
348
|
+
* import { Controller } from 'lumen-framework';
|
|
349
|
+
*
|
|
350
|
+
* const UNSAFE_METHODS = /(?:POST|PATCH|DELETE)/i;
|
|
351
|
+
*
|
|
352
|
+
* function isAdmin(user) {
|
|
353
|
+
* if (user) {
|
|
354
|
+
* return user.isAdmin;
|
|
355
|
+
* }
|
|
356
|
+
*
|
|
357
|
+
* return false;
|
|
358
|
+
* }
|
|
359
|
+
*
|
|
360
|
+
* async function authentication(request) {
|
|
361
|
+
* const { method, currentUser } = request;
|
|
362
|
+
* const isUnsafe = UNSAFE_METHODS.test(method);
|
|
363
|
+
*
|
|
364
|
+
* if (isUnsafe && !isAdmin(currentUser)) {
|
|
365
|
+
* return false; // 401 Unauthorized if the current user is not an admin.
|
|
366
|
+
* }
|
|
367
|
+
* }
|
|
368
|
+
*
|
|
369
|
+
* class PostsController extends Controller {
|
|
370
|
+
* beforeAction = [
|
|
371
|
+
* authentication
|
|
372
|
+
* ];
|
|
373
|
+
* }
|
|
374
|
+
*
|
|
375
|
+
* export default PostsController;
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* @property beforeAction
|
|
379
|
+
* @type {Array}
|
|
380
|
+
* @default []
|
|
381
|
+
* @public
|
|
382
|
+
*/
|
|
383
|
+
beforeAction: Array<Controller$beforeAction>;
|
|
384
|
+
/**
|
|
385
|
+
* Functions to execute on each request handled by a `Controller` after the
|
|
386
|
+
* `Controller` action is executed.
|
|
387
|
+
*
|
|
388
|
+
* Functions called from the `afterAction` hook will have `request` and
|
|
389
|
+
* `response` objects passed as arguments as well as a third `payload`
|
|
390
|
+
* argument. The `payload` argument is a reference to the resolved data of
|
|
391
|
+
* the Controller action that was called within the current `request` /
|
|
392
|
+
* `response` cycle. You need to explicitly return this `payload` in order for
|
|
393
|
+
* the afterAction to resolve with it's data. If you return a modified value
|
|
394
|
+
* from a function added to the `afterAction` hook, that value will be used
|
|
395
|
+
* instead of the resolved data from the preceding Controller action.
|
|
396
|
+
* Subsequent hooks called from an `afterAction` hook will will use the value
|
|
397
|
+
* returned or resolved from the preceding hook. This makes `afterAction` a
|
|
398
|
+
* great place to modify the data you are sending back to the client.
|
|
399
|
+
*
|
|
400
|
+
* **Example:**
|
|
401
|
+
*
|
|
402
|
+
* ```javascript
|
|
403
|
+
* import { Controller } from 'lumen-framework';
|
|
404
|
+
*
|
|
405
|
+
* async function addCopyright(request, response, payload) {
|
|
406
|
+
* const { action } = request;
|
|
407
|
+
*
|
|
408
|
+
* if (payload && action !== preflight) {
|
|
409
|
+
* return {
|
|
410
|
+
* ...payload,
|
|
411
|
+
* meta: {
|
|
412
|
+
* copyright: '2016 (c) Postlight'
|
|
413
|
+
* }
|
|
414
|
+
* };
|
|
415
|
+
* }
|
|
416
|
+
*
|
|
417
|
+
* return payload;
|
|
418
|
+
* }
|
|
419
|
+
*
|
|
420
|
+
* class ApplicationController extends Controller {
|
|
421
|
+
* afterAction = [
|
|
422
|
+
* addCopyright
|
|
423
|
+
* ];
|
|
424
|
+
* }
|
|
425
|
+
*
|
|
426
|
+
* export default ApplicationController;
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* @property afterAction
|
|
430
|
+
* @type {Array}
|
|
431
|
+
* @default []
|
|
432
|
+
* @public
|
|
433
|
+
*/
|
|
434
|
+
afterAction: Array<Controller$afterAction>;
|
|
435
|
+
/**
|
|
436
|
+
* The default amount of items to include per each response of the index
|
|
437
|
+
* action if a `?page[size]` query parameter is not specified.
|
|
438
|
+
*
|
|
439
|
+
* @property defaultPerPage
|
|
440
|
+
* @type {Number}
|
|
441
|
+
* @default 25
|
|
442
|
+
* @public
|
|
443
|
+
*/
|
|
444
|
+
defaultPerPage: number;
|
|
445
|
+
/**
|
|
446
|
+
* The resolved Model for a Controller instance.
|
|
447
|
+
*
|
|
448
|
+
* @property model
|
|
449
|
+
* @type {Model}
|
|
450
|
+
* @private
|
|
451
|
+
*/
|
|
452
|
+
model: ModelClass<Model>;
|
|
453
|
+
/**
|
|
454
|
+
* A reference to the root Controller for the namespace that a Controller
|
|
455
|
+
* instance is a member of.
|
|
456
|
+
*
|
|
457
|
+
* @property parent
|
|
458
|
+
* @type {?Controller}
|
|
459
|
+
* @private
|
|
460
|
+
*/
|
|
461
|
+
parent: Controller | null;
|
|
462
|
+
/**
|
|
463
|
+
* The namespace that a Controller instance is a member of.
|
|
464
|
+
*
|
|
465
|
+
* @property namespace
|
|
466
|
+
* @type {String}
|
|
467
|
+
* @private
|
|
468
|
+
*/
|
|
469
|
+
namespace: string;
|
|
470
|
+
/**
|
|
471
|
+
* The resolved Serializer for a Controller instance.
|
|
472
|
+
*
|
|
473
|
+
* @property serializer
|
|
474
|
+
* @type {Serializer}
|
|
475
|
+
* @private
|
|
476
|
+
*/
|
|
477
|
+
serializer: Serializer<Model>;
|
|
478
|
+
/**
|
|
479
|
+
* A Map instance containing a reference to all the Controller within an
|
|
480
|
+
* Application instance.
|
|
481
|
+
*
|
|
482
|
+
* @property controllers
|
|
483
|
+
* @type {Map}
|
|
484
|
+
* @private
|
|
485
|
+
*/
|
|
486
|
+
controllers: Map<string, Controller>;
|
|
487
|
+
/**
|
|
488
|
+
* A boolean value representing whether or not a Controller instance has a
|
|
489
|
+
* Model.
|
|
490
|
+
*
|
|
491
|
+
* @property hasModel
|
|
492
|
+
* @type {Boolean}
|
|
493
|
+
* @private
|
|
494
|
+
*/
|
|
495
|
+
hasModel: boolean;
|
|
496
|
+
/**
|
|
497
|
+
* A boolean value representing whether or not a Controller instance is within
|
|
498
|
+
* a namespace.
|
|
499
|
+
*
|
|
500
|
+
* @property hasNamespace
|
|
501
|
+
* @type {Boolean}
|
|
502
|
+
* @private
|
|
503
|
+
*/
|
|
504
|
+
hasNamespace: boolean;
|
|
505
|
+
/**
|
|
506
|
+
* A boolean value representing whether or not a Controller instance has a
|
|
507
|
+
* Serializer.
|
|
508
|
+
*
|
|
509
|
+
* @property hasSerializer
|
|
510
|
+
* @type {Boolean}
|
|
511
|
+
* @private
|
|
512
|
+
*/
|
|
513
|
+
hasSerializer: boolean;
|
|
514
|
+
constructor({ model, namespace, serializer }: Controller$opts);
|
|
515
|
+
/**
|
|
516
|
+
* This method supports filtering, sorting, pagination, including
|
|
517
|
+
* relationships, and sparse fieldsets via query parameters. For more
|
|
518
|
+
* information, see the [fetching resources](https://goo.gl/q7FVgZ) section of
|
|
519
|
+
* the JSON API specification.
|
|
520
|
+
*
|
|
521
|
+
* @method index
|
|
522
|
+
* @param {Request} request - The request object.
|
|
523
|
+
* @param {Response} response - The response object.
|
|
524
|
+
* @return {Promise} Resolves with an array of Model instances.
|
|
525
|
+
* @public
|
|
526
|
+
*/
|
|
527
|
+
index(req: Request): Query<Array<Model>>;
|
|
528
|
+
/**
|
|
529
|
+
* This method supports including relationships, and sparse fieldsets via
|
|
530
|
+
* query parameters. For more information, see the [fetching resources](
|
|
531
|
+
* https://goo.gl/q7FVgZ) section of the JSON API specification.
|
|
532
|
+
*
|
|
533
|
+
* @method show
|
|
534
|
+
* @param {Request} request - The request object.
|
|
535
|
+
* @param {Response} response - The response object.
|
|
536
|
+
* @return {Promise} Resolves with a Model instance with the id equal to the
|
|
537
|
+
* id url parameter.
|
|
538
|
+
* @public
|
|
539
|
+
*/
|
|
540
|
+
show(req: Request): Query<Model>;
|
|
541
|
+
/**
|
|
542
|
+
* Create and return a single Model instance that the Controller instance
|
|
543
|
+
* represents. For more information, see the [creating resources](
|
|
544
|
+
* https://goo.gl/4Obc9t) section of the JSON API specification.
|
|
545
|
+
*
|
|
546
|
+
* @method create
|
|
547
|
+
* @param {Request} request - The request object.
|
|
548
|
+
* @param {Response} response - The response object.
|
|
549
|
+
* @return {Promise} Resolves with the newly created Model instance.
|
|
550
|
+
* @public
|
|
551
|
+
*/
|
|
552
|
+
create(req: Request, res: Response): Promise<Model>;
|
|
553
|
+
/**
|
|
554
|
+
* Update and return a single Model instance that the Controller instance
|
|
555
|
+
* represents. For more information, see the [updating resources](
|
|
556
|
+
* https://goo.gl/o2ZdOR)section of the JSON API specification.
|
|
557
|
+
*
|
|
558
|
+
* @method update
|
|
559
|
+
* @param {Request} request - The request object.
|
|
560
|
+
* @param {Response} response - The response object.
|
|
561
|
+
* @return {Promise} Resolves with the updated Model if changes occur.
|
|
562
|
+
* Resolves with the number `204` if no changes occur.
|
|
563
|
+
* @public
|
|
564
|
+
*/
|
|
565
|
+
update(req: Request): Promise<number | Model>;
|
|
566
|
+
/**
|
|
567
|
+
* Destroy a single Model instance that the Controller instance represents.
|
|
568
|
+
* For more information, see the [deleting resources](https://goo.gl/nUZn8t)
|
|
569
|
+
* section of the JSON API specification.
|
|
570
|
+
*
|
|
571
|
+
* @method destroy
|
|
572
|
+
* @param {Request} request - The request object.
|
|
573
|
+
* @param {Response} response - The response object.
|
|
574
|
+
* @return {Promise} Resolves with the number `204`.
|
|
575
|
+
* @public
|
|
576
|
+
*/
|
|
577
|
+
destroy(req: Request): Promise<number>;
|
|
578
|
+
/**
|
|
579
|
+
* Respond to HEAD or OPTIONS requests.
|
|
580
|
+
*
|
|
581
|
+
* @method preflight
|
|
582
|
+
* @param {Request} request - The request object.
|
|
583
|
+
* @param {Response} response - The response object.
|
|
584
|
+
* @return {Promise} Resolves with the number `204`.
|
|
585
|
+
* @public
|
|
586
|
+
*/
|
|
587
|
+
preflight(): Promise<number>;
|
|
588
|
+
}
|
|
589
|
+
export default Controller;
|
|
590
|
+
export { BUILT_IN_ACTIONS } from './constants';
|
|
591
|
+
export type { Controller$opts, Controller$builtIn, Controller$beforeAction, Controller$afterAction } from './interfaces';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Model, ModelClass, Query } from '../database';
|
|
2
|
+
import type { Request, Response } from '../server';
|
|
3
|
+
import type Serializer from '../serializer';
|
|
4
|
+
export type Controller$opts = {
|
|
5
|
+
model?: ModelClass<Model>;
|
|
6
|
+
namespace?: string;
|
|
7
|
+
serializer?: Serializer<Model>;
|
|
8
|
+
};
|
|
9
|
+
export type Controller$builtIn = 'show' | 'index' | 'create' | 'update' | 'destroy';
|
|
10
|
+
export type Controller$beforeAction = (request: Request, response: Response) => Promise<unknown>;
|
|
11
|
+
export type Controller$afterAction = (request: Request, response: Response, responseData?: unknown) => Promise<unknown>;
|
|
12
|
+
export type Controller$findOne<T extends Model> = (request: Request) => Query<T>;
|
|
13
|
+
export type Controller$findMany<T extends Model> = (request: Request) => Query<Array<T>>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ModelClass } from '../../database';
|
|
2
|
+
import type { Request$params } from '../../server';
|
|
3
|
+
type Controller$query = {
|
|
4
|
+
id?: number | string | Buffer;
|
|
5
|
+
filter?: Record<string, unknown>;
|
|
6
|
+
select: Array<string>;
|
|
7
|
+
page?: number;
|
|
8
|
+
limit?: number;
|
|
9
|
+
sort?: [string, string];
|
|
10
|
+
include: Record<string, Array<string>>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
export default function paramsToQuery(model: ModelClass, { id, page, sort, filter, fields, include }: Request$params): Controller$query;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function createNormalizer(type: string): (value: unknown) => unknown;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type Model from '../../model';
|
|
2
|
+
import type { Attribute$meta } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* @private
|
|
5
|
+
*/
|
|
6
|
+
export default function createSetter({ key, nullable, normalize, defaultValue }: Attribute$meta & {
|
|
7
|
+
normalize: (value: unknown) => unknown;
|
|
8
|
+
}): (this: Model, value?: unknown) => void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Model } from '../index';
|
|
2
|
+
declare class ChangeSet extends Map<string, any> {
|
|
3
|
+
isPersisted: boolean;
|
|
4
|
+
constructor(data?: Record<string, unknown>);
|
|
5
|
+
set(key: string, value: any): this;
|
|
6
|
+
persist(group?: Array<ChangeSet>): this;
|
|
7
|
+
unpersist(): this;
|
|
8
|
+
applyTo(target: Model): ChangeSet;
|
|
9
|
+
}
|
|
10
|
+
export default ChangeSet;
|