Skip to content

Commit

Permalink
feat: allow hooks for global and bulk smart action (#769)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: record is no longer send to the hook middleware & values option on smart action is no longer supported
  • Loading branch information
arnaud-moncel authored Jul 9, 2021
1 parent 3670325 commit 14c26cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@babel/runtime": "7.10.1",
"bluebird": "2.9.25",
"core-js": "3.6.5",
"forest-express": "9.0.0-beta.9",
"forest-express": "9.0.0-beta.11",
"http-errors": "1.6.1",
"lodash": "4.17.21",
"moment": "2.19.4",
Expand Down
69 changes: 64 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,65 @@ export interface User {
renderingId: number;
}

export interface ForestRequest extends Request {
user: User,
}

// Base attributes for actions requests (content of request.data.body.attributes)
interface ActionRequestAttributes {
collection_name: string,
ids: string[],
parent_collection_name: string,
parent_collection_id: string,
parent_association_name: string,
all_records: boolean,
all_records_subset_query: Query,
all_records_ids_excluded: string[],
smart_action_id: string,
}

// Base body from requests for action routes / hooks
interface ActionRequestBody {
data: {
attributes: ActionRequestAttributes,
type: 'action-requests',
},
}

// Base body from requests for classic smart action routes
interface SmartActionRequestBody {
data: {
attributes: ActionRequestAttributes & { values: Record<string, any> },
type: 'custom-action-requests',
},
}

// Base body from requests for smart action hooks
interface SmartActionHookRequestBody {
data: {
attributes: ActionRequestAttributes & {
fields: SmartActionChangeHookField[],
changedField: string,
},
type: 'custom-action-hook-requests',
},
}

// Concrete smart action request for classic smart action routes
export interface SmartActionRequest extends ForestRequest {
body: SmartActionRequestBody,
}

// Request passed to smart action load hooks
export interface SmartActionLoadHookRequest extends ForestRequest {
body: ActionRequestBody,
}

// Request passed to smart action change hooks
export interface SmartActionChangeHookRequest extends ForestRequest {
body: SmartActionHookRequestBody,
}

// Everything related to Forest constants

export const PUBLIC_ROUTES: string[];
Expand All @@ -54,7 +113,7 @@ export class RecordGetter<M extends Sequelize.Model> extends AbstractRecordTool<

export class RecordsGetter<M extends Sequelize.Model> extends AbstractRecordTool<M> {
getAll(query: Query): Promise<M[]>;
getIdsFromRequest(request: Request): Promise<string[]>;
getIdsFromRequest(request: SmartActionRequest | SmartActionLoadHookRequest | SmartActionChangeHookRequest): Promise<string[]>;
}

export class RecordsCounter<M extends Sequelize.Model> extends AbstractRecordTool<M> {
Expand Down Expand Up @@ -218,16 +277,16 @@ export interface SmartActionLoadHookField extends SmartActionHookField {
position: number,
}

export interface SmartActionLoadHook<M extends Sequelize.Model = any> {
(context: { fields: SmartActionLoadHookField[], record: M }): SmartActionLoadHookField[]
export interface SmartActionLoadHook {
(context: { fields: SmartActionLoadHookField[], request: SmartActionLoadHookRequest }): SmartActionLoadHookField[]
}

export interface SmartActionChangeHookField extends SmartActionHookField {
previousValue: any,
}

export interface SmartActionChangeHook<M extends Sequelize.Model = any> {
(context: { fields: SmartActionChangeHookField[], record: M, changedField: SmartActionChangeHookField }): SmartActionChangeHookField[]
export interface SmartActionChangeHook {
(context: { fields: SmartActionChangeHookField[], changedField: SmartActionChangeHookField, request: SmartActionChangeHookRequest }): SmartActionChangeHookField[]
}

export interface SmartActionHooks {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5067,10 +5067,10 @@ for-in@^1.0.2:
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
integrity sha1-gQaNKVqBQuwKxybG4iAMMPttXoA=

[email protected].9:
version "9.0.0-beta.9"
resolved "https://registry.yarnpkg.com/forest-express/-/forest-express-9.0.0-beta.9.tgz#c92f7ea63ddb160667c419a60bfb35b8b97187fd"
integrity sha512-P6hSk0JZNee3e/eCTbVxzhQd5te+Q77xBuvv4jN5sg60S7ZbYLKNtgm9rpyLjs1iclLZMx/sHn/EbyHb48drwA==
[email protected].11:
version "9.0.0-beta.11"
resolved "https://registry.yarnpkg.com/forest-express/-/forest-express-9.0.0-beta.11.tgz#99b553c60ac6da76567c13f38fb5a78ad17d0e55"
integrity sha512-ufU/dIe9WTlQGCR2KJIS/wAXvvEb300Xk2GL1iXHSr4uny8zFlnPXwZlZHdWSC8FQZlnclvUj1xfsanf/WWt1A==
dependencies:
"@babel/runtime" "7.10.1"
base32-encode "1.1.1"
Expand Down

0 comments on commit 14c26cd

Please sign in to comment.