Skip to content

Commit 3a5b8d3

Browse files
committed
Changes
1 parent 1821b93 commit 3a5b8d3

File tree

9 files changed

+52
-38
lines changed

9 files changed

+52
-38
lines changed

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ notice.log
1414
info.log
1515
warning.log
1616
.env
17-
uploads
17+
uploads
18+
storage

docs/context/introduction.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Wertik-js provides these built-in context properties
3434
- res: Res object to send custom response, Varies with GraphQL and RestAPI
3535

3636
#### Create Context Handler
37-
If you want to pass a function that executes while context creation, You can pass a function in your configuration, This function supports async await. `configuration.context.createContext`, When this function will be called, two paramters will be passed,
37+
If you want to pass a function that executes while context creation, You can pass a function in your configuration, This function supports async await. `configuration.context.requestContext`, When this function will be called, two paramters will be passed,
3838

3939
- mode: This will be restApi or graphql, When this function runs in graphql then value will be graphql and for restApi it becomes restApi.
4040
- context: For GraphQL this value is context object and for restApi it is req object so that you can access current context
@@ -45,15 +45,15 @@ For GraphQL resolver:
4545

4646
```javascript
4747
function (_, args, context,info) {
48-
console.log(context.createContext)
48+
console.log(context.requestContext)
4949
}
5050
```
5151

5252
For RestAPI handler:
5353

5454
```javascript
5555
function (req,res) {
56-
console.log(req.createContext)
56+
console.log(req.requestContext)
5757
}
5858
```
5959

docs/getting-started/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
data: {
2222
myName: "My powerful app",
2323
},
24-
createContext: async function (mode, context) {
24+
requestContext: async function (mode, context) {
2525
return {
2626
value: "Value 1",
2727
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wertik-js",
3-
"version": "1.9.9",
3+
"version": "1.9.90",
44
"main": "lib/main.js",
55
"repository": "https://github.com/Uconnect-Technologies/wertik-js.git",
66
"welcomeResponse": "Welcome to wertik, You are successfully running Wertik.",

src/framework/defaults/defaultConfigurations/defaultConfiguration.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ export default {
99
dbHost: "localhost",
1010
dbPort: "3306",
1111
},
12-
1312
frontendAppUrl: "http://localhost:8080/",
1413
frontendAppActivationUrl: "http://localhost:8080/activate-account",
1514
frontendAppPasswordResetUrl: "http://localhost:8080/reset-password",
1615
context: {
17-
data: {
18-
myName: "My powerful app",
16+
initializeContext: function (mode, context) {
17+
return {
18+
someKey: "somekeyvalue"
19+
}
1920
},
20-
createContext: async function (mode, context) {
21+
requestContext: async function (mode, context) {
2122
return {
2223
value: "Value 1",
2324
};
@@ -77,11 +78,6 @@ export default {
7778
restApi: {
7879
endpoints: [
7980
{
80-
docs: {
81-
title: "Apple module response.",
82-
description: "Just a message.",
83-
response: `@apiSuccess {Object} returns an object {message: true}.`,
84-
},
8581
path: "/apple/response",
8682
methodType: "get",
8783
handler: function (req, res) {

src/framework/graphql/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ const { ApolloServer } = require("apollo-server-express");
1717
export default async function (options: IGraphQLInitialize) {
1818
const { mailerInstance, configuration, dbTables, models, sendEmail, emailTemplates, database, websockets, logger } = options;
1919
const apolloGraphqlOptions = get(options, "apolloGraphqlOptions", defaultApolloGraphqlOptions);
20+
let initializeContext = get(configuration, "context.initializeContext", async function () {});
21+
initializeContext = await initializeContext("graphql",{
22+
dbTables,
23+
models,
24+
database,
25+
});
2026
let { graphql } = configuration;
21-
const port = get(graphql, "port", 4000);
2227
if (get(graphql, "disable", true) === true) {
2328
return null;
2429
}
@@ -54,10 +59,10 @@ export default async function (options: IGraphQLInitialize) {
5459
res,
5560
websockets,
5661
logger,
57-
...get(configuration.context, "data", {}),
62+
initializeContext: initializeContext
5863
};
59-
let createContext = await get(configuration.context, "createContext", () => {})("graphql", cxt);
60-
cxt["createContext"] = createContext;
64+
let requestContext = await get(configuration.context, "requestContext", () => {})("graphql", cxt);
65+
cxt["requestContext"] = requestContext;
6166
return cxt;
6267
},
6368
...apolloGraphqlOptions,

src/framework/restApi/customApi.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import restApiErrorResponse from "./restApiErrorResponse";
33
import restApiSuccessResponse from "./restApiSuccessResponse";
44

55
export default (expressApp, restApiEndpointsElement, module) => {
6-
const versionPath = "api/v1";
76
const type = get(restApiEndpointsElement, "methodType", "get");
87
const handler = get(restApiEndpointsElement, "handler", null);
98
const path = get(restApiEndpointsElement, "path", "");
109
const types = ["get", "post", "put", "delete", "copy", "head", "options", "link", "unlink", "purge", "lock", "unlock", "view"];
1110

12-
if (types.indexOf(type) > -1) {
13-
let apiPath = `/${versionPath}/${kebabCase(module.name)}/${path}`;
11+
if (types.indexOf(type) > -1 && path.length>0) {
12+
let apiPath = `${path}`;
1413
let find = "//";
1514
let re = new RegExp(find, "g");
1615
apiPath = apiPath.replace(re, "/");

src/framework/restApi/index.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,32 @@ import customApi from "./customApi";
4242
*/
4343

4444
//expressApp,configuration,dbTables, models, allEmailTemplates,sendEmail,database,WertikEventEmitter
45-
export default function (options: IRestApiInitialize) {
46-
const { configuration, dbTables, models, sendEmail, emailTemplates, expressApp, database, runEvent, multerInstance, mailerInstance, websockets, logger } = options;
45+
export default async function (options: IRestApiInitialize) {
46+
const {
47+
configuration,
48+
dbTables,
49+
models,
50+
sendEmail,
51+
emailTemplates,
52+
expressApp,
53+
database,
54+
runEvent,
55+
multerInstance,
56+
mailerInstance,
57+
websockets,
58+
logger,
59+
} = options;
4760
let { restApi } = configuration;
4861
if (get(restApi, "disable", true) === true) {
4962
return expressApp;
5063
}
64+
let initializeContext = get(configuration, "context.initializeContext", async function () {});
65+
initializeContext = await initializeContext("restApi",{
66+
dbTables,
67+
models,
68+
expressApp,
69+
database,
70+
});
5171
expressApp.use(cors());
5272
expressApp.use(bodyParser.urlencoded({ extended: false }));
5373
expressApp.use(bodyParser.json());
@@ -70,19 +90,20 @@ export default function (options: IRestApiInitialize) {
7090
req.dbTables = dbTables;
7191
req.models = models;
7292
req.websockets = websockets;
73-
req.context = get(configuration.context, "data", {});
93+
// req.context = get(configuration.context, "data", {});
7494
req.sendEmail = sendEmail;
7595
req.emailTemplates = emailTemplates;
7696
req.multerInstance = multerInstance;
7797
req.logger = logger;
78-
let createContext = await get(configuration.context, "createContext", () => {})("restApi", req);
79-
req.createContext = createContext;
98+
let requestContext = await get(configuration.context, "requestContext", () => {})("restApi", req);
99+
req.requestContext = requestContext;
100+
req.initializeContext = initializeContext;
80101
next();
81102
});
82103

83104
require("./versions/v1/loadAllModules").default(expressApp, configuration, customApi);
84105

85-
expressApp.get("/", (req, res) => {
106+
expressApp.get("/w/info", (req, res) => {
86107
res.status(200).json({
87108
message: require("./../../../package.json").welcomeResponse,
88109
version: require("./../../../package.json").version,

src/framework/types/configuration.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,9 @@ export interface IConfigurationCustomModuleGraphql {
3939
query: IConfigurationCustomModuleGraphqlQuery;
4040
}
4141

42-
export interface IConfigurationCustomModuleRestApiDocs {
43-
description: string;
44-
params: string;
45-
response: string;
46-
title: string;
47-
}
48-
4942
export interface IConfigurationCustomModuleRestApiEndpoint {
5043
path: string;
5144
methodType: string;
52-
docs: IConfigurationCustomModuleRestApiDocs;
5345
handler: Function;
5446
}
5547

@@ -191,8 +183,8 @@ export interface IConfigurationEvents {
191183
}
192184

193185
export interface IConfigurationContext {
194-
data: Object;
195-
createContext: Function;
186+
initializeContext: Function;
187+
requestContext: Function;
196188
}
197189

198190
export interface IConfigurationRestApi {

0 commit comments

Comments
 (0)