@lowdefy/api 4.0.0-alpha.1 → 4.0.0-alpha.12

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.
Files changed (42) hide show
  1. package/dist/{routes/rootConfig/getHome.js → auth/callbacks/createCallbackPlugins.js} +9 -11
  2. package/dist/auth/callbacks/createCallbacks.js +43 -0
  3. package/dist/auth/callbacks/createJWTCallback.js +38 -0
  4. package/dist/auth/callbacks/createRedirectCallback.js +36 -0
  5. package/dist/auth/callbacks/createSessionCallback.js +38 -0
  6. package/dist/auth/callbacks/createSignInCallback.js +40 -0
  7. package/dist/auth/createProviders.js +26 -0
  8. package/dist/auth/events/createCreateUserEvent.js +33 -0
  9. package/dist/auth/events/createEventPlugins.js +23 -0
  10. package/dist/auth/events/createEvents.js +55 -0
  11. package/dist/auth/events/createLinkAccountEvent.js +34 -0
  12. package/dist/auth/events/createSessionEvent.js +34 -0
  13. package/dist/auth/events/createSignInEvent.js +36 -0
  14. package/dist/auth/events/createSignOutEvent.js +34 -0
  15. package/dist/auth/events/createUpdateUserEvent.js +33 -0
  16. package/dist/auth/getNextAuthConfig.js +58 -0
  17. package/dist/context/createApiContext.js +10 -7
  18. package/dist/context/createAuthorize.js +6 -2
  19. package/dist/context/{readConfigFile.js → createReadConfigFile.js} +1 -1
  20. package/dist/context/errors.js +9 -9
  21. package/dist/index.js +4 -3
  22. package/dist/routes/page/getPageConfig.js +5 -2
  23. package/dist/routes/request/authorizeRequest.js +1 -1
  24. package/dist/routes/request/callRequest.js +2 -2
  25. package/dist/routes/request/callRequestResolver.js +1 -1
  26. package/dist/routes/request/checkConnectionRead.js +1 -1
  27. package/dist/routes/request/checkConnectionWrite.js +1 -1
  28. package/dist/routes/request/evaluateOperators.js +26 -34
  29. package/dist/routes/request/getConnection.js +1 -1
  30. package/dist/routes/request/getConnectionConfig.js +1 -1
  31. package/dist/routes/request/getRequestConfig.js +1 -1
  32. package/dist/routes/request/getRequestResolver.js +1 -1
  33. package/dist/routes/request/validateSchemas.js +1 -1
  34. package/dist/routes/rootConfig/{findHome.js → getHomeAndMenus.js} +17 -8
  35. package/dist/routes/rootConfig/getLowdefyGlobal.js +2 -3
  36. package/dist/routes/rootConfig/getRootConfig.js +5 -9
  37. package/dist/routes/rootConfig/menus/filterMenuList.js +1 -1
  38. package/dist/routes/rootConfig/menus/filterMenus.js +2 -2
  39. package/dist/routes/rootConfig/menus/getMenus.js +2 -2
  40. package/dist/test/testContext.js +12 -21
  41. package/package.json +14 -12
  42. package/dist/context/createContext.js +0 -46
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -12,14 +12,12 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import findHome from './findHome.js';
16
- import getMenus from './menus/getMenus.js';
17
- async function getHome(context) {
18
- // TODO: We can optimise here as we don't need to read menus if homepageId is configured
19
- // but not sure if it is worth the added complexity
20
- const menus = await getMenus(context);
21
- return findHome(context, {
22
- menus
23
- });
15
+ */ function createCallbackPlugins({ authConfig , plugins , type }) {
16
+ return authConfig.callbacks.map((callbackConfig)=>({
17
+ fn: plugins.callbacks[callbackConfig.type],
18
+ properties: callbackConfig.properties
19
+ })
20
+ ).filter((callback)=>callback.fn.meta.type === type
21
+ );
24
22
  }
25
- export default getHome;
23
+ export default createCallbackPlugins;
@@ -0,0 +1,43 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createJWTCallback from './createJWTCallback.js';
16
+ import createRedirectCallback from './createRedirectCallback.js';
17
+ import createSessionCallback from './createSessionCallback.js';
18
+ import createSignInCallback from './createSignInCallback.js';
19
+ function createCallbacks({ authConfig , plugins }) {
20
+ const callbacks = {
21
+ session: createSessionCallback({
22
+ authConfig,
23
+ plugins
24
+ })
25
+ };
26
+ const jwt = createJWTCallback({
27
+ authConfig,
28
+ plugins
29
+ });
30
+ if (jwt) callbacks.jwt = jwt;
31
+ const redirect = createRedirectCallback({
32
+ authConfig,
33
+ plugins
34
+ });
35
+ if (redirect) callbacks.redirect = redirect;
36
+ const signIn = createSignInCallback({
37
+ authConfig,
38
+ plugins
39
+ });
40
+ if (signIn) callbacks.signIn = signIn;
41
+ return callbacks;
42
+ }
43
+ export default createCallbacks;
@@ -0,0 +1,38 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCallbackPlugins from './createCallbackPlugins.js';
16
+ function createJWTCallback({ authConfig , plugins }) {
17
+ const jwtCallbackPlugins = createCallbackPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'jwt'
21
+ });
22
+ if (jwtCallbackPlugins.length === 0) return undefined;
23
+ async function jwtCallback({ token , user , account , profile , isNewUser }) {
24
+ for (const plugin of jwtCallbackPlugins){
25
+ token = await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ account,
28
+ isNewUser,
29
+ profile,
30
+ token,
31
+ user
32
+ });
33
+ }
34
+ return token;
35
+ }
36
+ return jwtCallback;
37
+ }
38
+ export default createJWTCallback;
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCallbackPlugins from './createCallbackPlugins.js';
16
+ function createRedirectCallback({ authConfig , plugins }) {
17
+ const redirectCallbackPlugins = createCallbackPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'redirect'
21
+ });
22
+ if (redirectCallbackPlugins.length === 0) return undefined;
23
+ if (redirectCallbackPlugins.length !== 1) {
24
+ throw new Error('More than one auth redirect callbacks are configured. Only one is allowed.');
25
+ }
26
+ const [plugin] = redirectCallbackPlugins;
27
+ async function redirectCallback({ url , baseUrl }) {
28
+ return plugin.fn({
29
+ properties: plugin.properties ?? {},
30
+ baseUrl,
31
+ url
32
+ });
33
+ }
34
+ return redirectCallback;
35
+ }
36
+ export default createRedirectCallback;
@@ -0,0 +1,38 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCallbackPlugins from './createCallbackPlugins.js';
16
+ function createSessionCallback({ authConfig , plugins }) {
17
+ const sessionCallbackPlugins = createCallbackPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'session'
21
+ });
22
+ async function sessionCallback({ session , token , user }) {
23
+ if (token) {
24
+ session.user.sub = token.sub;
25
+ }
26
+ for (const plugin of sessionCallbackPlugins){
27
+ session = await plugin.fn({
28
+ properties: plugin.properties ?? {},
29
+ session,
30
+ token,
31
+ user
32
+ });
33
+ }
34
+ return session;
35
+ }
36
+ return sessionCallback;
37
+ }
38
+ export default createSessionCallback;
@@ -0,0 +1,40 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCallbackPlugins from './createCallbackPlugins.js';
16
+ function createSignInCallback({ authConfig , plugins }) {
17
+ const signInCallbackPlugins = createCallbackPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'signIn'
21
+ });
22
+ if (signInCallbackPlugins.length === 0) return undefined;
23
+ async function signInCallback({ account , credentials , email , profile , user }) {
24
+ let allowSignIn = true;
25
+ for (const plugin of signInCallbackPlugins){
26
+ allowSignIn = await plugin.fn({
27
+ properties: plugin.properties ?? {},
28
+ account,
29
+ credentials,
30
+ email,
31
+ profile,
32
+ user
33
+ });
34
+ if (allowSignIn === false) break;
35
+ }
36
+ return allowSignIn;
37
+ }
38
+ return signInCallback;
39
+ }
40
+ export default createSignInCallback;
@@ -0,0 +1,26 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ // TODO: docs:
16
+ // Callback url to configure with provider will be: {{ protocol }}{{ host }}/api/auth/callback/{{ providerId }}
17
+ // This depends on providerId, which might cause some issues if users copy an example and change the id.
18
+ // We need to allow users to configure ids, since they might have more than one of the same type.
19
+ function createProviders({ authConfig , plugins }) {
20
+ return authConfig.providers.map((providerConfig)=>plugins.providers[providerConfig.type]({
21
+ ...providerConfig.properties,
22
+ id: providerConfig.id
23
+ })
24
+ );
25
+ }
26
+ export default createProviders;
@@ -0,0 +1,33 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createCreateUserEvent({ authConfig , plugins }) {
17
+ const createUserPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'createUser'
21
+ });
22
+ if (createUserPlugins.length === 0) return undefined;
23
+ async function createUserEvent({ user }) {
24
+ for (const plugin of createUserPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ user
28
+ });
29
+ }
30
+ }
31
+ return createUserEvent;
32
+ }
33
+ export default createCreateUserEvent;
@@ -0,0 +1,23 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ function createEventPlugins({ authConfig , plugins , type }) {
16
+ return authConfig.events.map((eventConfig)=>({
17
+ fn: plugins.events[eventConfig.type],
18
+ properties: eventConfig.properties
19
+ })
20
+ ).filter((event)=>event.fn.meta.type === type
21
+ );
22
+ }
23
+ export default createEventPlugins;
@@ -0,0 +1,55 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createCreateUserEvent from './createCreateUserEvent.js';
16
+ import createLinkAccountEvent from './createLinkAccountEvent.js';
17
+ import createSessionEvent from './createSessionEvent.js';
18
+ import createSignInEvent from './createSignInEvent.js';
19
+ import createSignOutEvent from './createSignOutEvent.js';
20
+ import createUpdateUserEvent from './createUpdateUserEvent.js';
21
+ function createEvents({ authConfig , plugins }) {
22
+ const events = {};
23
+ const createUser = createCreateUserEvent({
24
+ authConfig,
25
+ plugins
26
+ });
27
+ if (createUser) events.createUser = createUser;
28
+ const linkAccount = createLinkAccountEvent({
29
+ authConfig,
30
+ plugins
31
+ });
32
+ if (linkAccount) events.linkAccount = linkAccount;
33
+ const session = createSessionEvent({
34
+ authConfig,
35
+ plugins
36
+ });
37
+ if (session) events.session = session;
38
+ const signIn = createSignInEvent({
39
+ authConfig,
40
+ plugins
41
+ });
42
+ if (signIn) events.signIn = signIn;
43
+ const signOut = createSignOutEvent({
44
+ authConfig,
45
+ plugins
46
+ });
47
+ if (signOut) events.signOut = signOut;
48
+ const updateUser = createUpdateUserEvent({
49
+ authConfig,
50
+ plugins
51
+ });
52
+ if (updateUser) events.updateUser = updateUser;
53
+ return events;
54
+ }
55
+ export default createEvents;
@@ -0,0 +1,34 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createLinkAccountEvent({ authConfig , plugins }) {
17
+ const linkAccountPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'linkAccount'
21
+ });
22
+ if (linkAccountPlugins.length === 0) return undefined;
23
+ async function linkAccountEvent({ account , user }) {
24
+ for (const plugin of linkAccountPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ account,
28
+ user
29
+ });
30
+ }
31
+ }
32
+ return linkAccountEvent;
33
+ }
34
+ export default createLinkAccountEvent;
@@ -0,0 +1,34 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createSessionEvent({ authConfig , plugins }) {
17
+ const sessionPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'session'
21
+ });
22
+ if (sessionPlugins.length === 0) return undefined;
23
+ async function sessionEvent({ session , token }) {
24
+ for (const plugin of sessionPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ session,
28
+ token
29
+ });
30
+ }
31
+ }
32
+ return sessionEvent;
33
+ }
34
+ export default createSessionEvent;
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createSignInEvent({ authConfig , plugins }) {
17
+ const signInPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'signIn'
21
+ });
22
+ if (signInPlugins.length === 0) return undefined;
23
+ async function signInEvent({ account , isNewUser , profile , user }) {
24
+ for (const plugin of signInPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ account,
28
+ isNewUser,
29
+ profile,
30
+ user
31
+ });
32
+ }
33
+ }
34
+ return signInEvent;
35
+ }
36
+ export default createSignInEvent;
@@ -0,0 +1,34 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createSignOutEvent({ authConfig , plugins }) {
17
+ const signInPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'signOut'
21
+ });
22
+ if (signInPlugins.length === 0) return undefined;
23
+ async function signInEvent({ session , token }) {
24
+ for (const plugin of signInPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ session,
28
+ token
29
+ });
30
+ }
31
+ }
32
+ return signInEvent;
33
+ }
34
+ export default createSignOutEvent;
@@ -0,0 +1,33 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import createEventPlugins from './createEventPlugins.js';
16
+ function createUpdateUserEvent({ authConfig , plugins }) {
17
+ const updateUserPlugins = createEventPlugins({
18
+ authConfig,
19
+ plugins,
20
+ type: 'updateUser'
21
+ });
22
+ if (updateUserPlugins.length === 0) return undefined;
23
+ async function updateUserEvent({ user }) {
24
+ for (const plugin of updateUserPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ user
28
+ });
29
+ }
30
+ }
31
+ return updateUserEvent;
32
+ }
33
+ export default createUpdateUserEvent;
@@ -0,0 +1,58 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { NodeParser } from '@lowdefy/operators';
16
+ import { getSecretsFromEnv } from '@lowdefy/node-utils';
17
+ import { _secret } from '@lowdefy/operators-js/operators/server';
18
+ import createCallbacks from './callbacks/createCallbacks.js';
19
+ import createEvents from './events/createEvents.js';
20
+ import createProviders from './createProviders.js';
21
+ const nextAuthConfig = {};
22
+ let initialized = false;
23
+ function getNextAuthConfig({ authJson , plugins }) {
24
+ if (initialized) return nextAuthConfig;
25
+ const secrets = getSecretsFromEnv();
26
+ const operatorsParser = new NodeParser({
27
+ operators: {
28
+ _secret
29
+ },
30
+ payload: {},
31
+ secrets,
32
+ user: {}
33
+ });
34
+ const { output: authConfig , errors: operatorErrors } = operatorsParser.parse({
35
+ input: authJson,
36
+ location: 'auth'
37
+ });
38
+ if (operatorErrors.length > 0) {
39
+ throw new Error(operatorErrors[0]);
40
+ }
41
+ nextAuthConfig.callbacks = createCallbacks({
42
+ authConfig,
43
+ plugins
44
+ });
45
+ nextAuthConfig.events = createEvents({
46
+ authConfig,
47
+ plugins
48
+ });
49
+ nextAuthConfig.providers = createProviders({
50
+ authConfig,
51
+ plugins
52
+ });
53
+ nextAuthConfig.session = authConfig.session;
54
+ nextAuthConfig.theme = authConfig.theme;
55
+ initialized = true;
56
+ return nextAuthConfig;
57
+ }
58
+ export default getNextAuthConfig;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -12,21 +12,24 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import createReadConfigFile from './readConfigFile.js';
16
- async function createApiContext({ buildDirectory , connections , logger , secrets }) {
15
+ */ import createAuthorize from './createAuthorize.js';
16
+ import createReadConfigFile from './createReadConfigFile.js';
17
+ async function createApiContext({ buildDirectory , connections , logger , operators , secrets , session , }) {
17
18
  const readConfigFile = createReadConfigFile({
18
19
  buildDirectory
19
20
  });
20
21
  const config = await readConfigFile('config.json');
21
22
  return {
22
- authenticated: false,
23
- authorize: ()=>true
24
- ,
23
+ authorize: createAuthorize({
24
+ session
25
+ }),
25
26
  config,
26
27
  connections,
27
28
  logger,
29
+ operators,
28
30
  readConfigFile,
29
- secrets
31
+ secrets,
32
+ user: session?.user
30
33
  };
31
34
  }
32
35
  export default createApiContext;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -13,7 +13,11 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ServerError } from '../context/errors.js';
16
- function createAuthorize({ authenticated =false , roles =[] }) {
16
+ function createAuthorize({ session }) {
17
+ // Next-auth getSession provides a session object if the user is authenticated
18
+ // else session will be null
19
+ const authenticated = !!session;
20
+ const roles = session?.user?.roles ?? [];
17
21
  function authorize({ auth }) {
18
22
  if (auth.public === true) return true;
19
23
  if (auth.public === false) {
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -19,26 +19,26 @@
19
19
  }
20
20
  };
21
21
  let ConfigurationError = class ConfigurationError extends Error {
22
- constructor(message1){
23
- super(message1);
22
+ constructor(message){
23
+ super(message);
24
24
  this.name = 'ConfigurationError';
25
25
  }
26
26
  };
27
27
  let RequestError = class RequestError extends Error {
28
- constructor(message2){
29
- super(message2);
28
+ constructor(message){
29
+ super(message);
30
30
  this.name = 'RequestError';
31
31
  }
32
32
  };
33
33
  let ServerError = class ServerError extends Error {
34
- constructor(message3){
35
- super(message3);
34
+ constructor(message){
35
+ super(message);
36
36
  this.name = 'ServerError';
37
37
  }
38
38
  };
39
39
  let TokenExpiredError = class TokenExpiredError extends Error {
40
- constructor(message4){
41
- super(message4);
40
+ constructor(message){
41
+ super(message);
42
42
  this.name = 'TokenExpiredError';
43
43
  }
44
44
  };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -14,8 +14,9 @@
14
14
  limitations under the License.
15
15
  */ import callRequest from './routes/request/callRequest.js';
16
16
  import createApiContext from './context/createApiContext.js';
17
- import getHome from './routes/rootConfig/getHome.js';
17
+ import getHomeAndMenus from './routes/rootConfig/getHomeAndMenus.js';
18
+ import getNextAuthConfig from './auth/getNextAuthConfig.js';
18
19
  import getPageConfig from './routes/page/getPageConfig.js';
19
20
  import getRootConfig from './routes/rootConfig/getRootConfig.js';
20
21
  import { AuthenticationError, ConfigurationError, RequestError, ServerError, TokenExpiredError } from './context/errors.js';
21
- export { callRequest, createApiContext, getHome, getPageConfig, getRootConfig, AuthenticationError, ConfigurationError, RequestError, ServerError, TokenExpiredError };
22
+ export { AuthenticationError, callRequest, ConfigurationError, createApiContext, getHomeAndMenus, getNextAuthConfig, getPageConfig, getRootConfig, RequestError, ServerError, TokenExpiredError };
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -14,7 +14,10 @@
14
14
  limitations under the License.
15
15
  */ async function getPageConfig({ authorize , readConfigFile }, { pageId }) {
16
16
  const pageConfig = await readConfigFile(`pages/${pageId}/${pageId}.json`);
17
- if (pageConfig && authorize(pageConfig)) return pageConfig;
17
+ if (pageConfig && authorize(pageConfig)) {
18
+ delete pageConfig.auth;
19
+ return pageConfig;
20
+ }
18
21
  return null;
19
22
  }
20
23
  export default getPageConfig;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ async function callRequest(context, { pageId , payload , requestId }) {
50
50
  connection,
51
51
  requestConfig
52
52
  });
53
- const { connectionProperties , requestProperties } = await evaluateOperators(context, {
53
+ const { connectionProperties , requestProperties } = evaluateOperators(context, {
54
54
  connectionConfig,
55
55
  payload: serializer.deserialize(payload),
56
56
  requestConfig
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -12,40 +12,32 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ // import { NodeParser } from '@lowdefy/operators';
16
- // import { RequestError } from '../../context/errors.js';
17
- // async function evaluateOperators({ secrets, user }, { connectionConfig, payload, requestConfig }) {
18
- // const operatorsParser = new NodeParser({
19
- // payload,
20
- // secrets,
21
- // user,
22
- // });
23
- // await operatorsParser.init();
24
- // const { output: connectionProperties, errors: connectionErrors } = operatorsParser.parse({
25
- // input: connectionConfig.properties || {},
26
- // location: connectionConfig.connectionId,
27
- // });
28
- // if (connectionErrors.length > 0) {
29
- // throw new RequestError(connectionErrors[0]);
30
- // }
31
- // const { output: requestProperties, errors: requestErrors } = operatorsParser.parse({
32
- // input: requestConfig.properties || {},
33
- // location: requestConfig.requestId,
34
- // });
35
- // if (requestErrors.length > 0) {
36
- // throw new RequestError(requestErrors[0]);
37
- // }
38
- // return {
39
- // connectionProperties,
40
- // requestProperties,
41
- // };
42
- // }
43
- async function evaluateOperators({ secrets , user }, { connectionConfig , payload , requestConfig }) {
15
+ */ import { NodeParser } from '@lowdefy/operators';
16
+ import { RequestError } from '../../context/errors.js';
17
+ function evaluateOperators({ operators , secrets , user }, { connectionConfig , payload , requestConfig }) {
18
+ const operatorsParser = new NodeParser({
19
+ operators,
20
+ payload,
21
+ secrets,
22
+ user
23
+ });
24
+ const { output: connectionProperties , errors: connectionErrors } = operatorsParser.parse({
25
+ input: connectionConfig.properties || {},
26
+ location: connectionConfig.connectionId
27
+ });
28
+ if (connectionErrors.length > 0) {
29
+ throw new RequestError(connectionErrors[0]);
30
+ }
31
+ const { output: requestProperties , errors: requestErrors } = operatorsParser.parse({
32
+ input: requestConfig.properties || {},
33
+ location: requestConfig.requestId
34
+ });
35
+ if (requestErrors.length > 0) {
36
+ throw new RequestError(requestErrors[0]);
37
+ }
44
38
  return {
45
- connectionProperties: connectionConfig.properties || {
46
- },
47
- requestProperties: requestConfig.properties || {
48
- }
39
+ connectionProperties,
40
+ requestProperties
49
41
  };
50
42
  }
51
43
  export default evaluateOperators;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -13,11 +13,17 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { get } from '@lowdefy/helpers';
16
- function findHome({ config }, { menus }) {
17
- if (get(config, 'homePageId')) {
16
+ import getMenus from './menus/getMenus.js';
17
+ async function getHomeAndMenus(context) {
18
+ const menus = await getMenus(context);
19
+ const homePageId = get(context.config, 'homePageId');
20
+ if (homePageId) {
18
21
  return {
19
- pageId: config.homePageId,
20
- configured: true
22
+ home: {
23
+ configured: true,
24
+ pageId: homePageId
25
+ },
26
+ menus
21
27
  };
22
28
  }
23
29
  let defaultMenu = menus.find((menu)=>menu.menuId === 'default'
@@ -41,8 +47,11 @@ function findHome({ config }, { menus }) {
41
47
  });
42
48
  }
43
49
  return {
44
- pageId,
45
- configured: false
50
+ home: {
51
+ configured: false,
52
+ pageId
53
+ },
54
+ menus
46
55
  };
47
56
  }
48
- export default findHome;
57
+ export default getHomeAndMenus;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -14,7 +14,6 @@
14
14
  limitations under the License.
15
15
  */ async function getLowdefyGlobal({ readConfigFile }) {
16
16
  const lowdefyGlobal = await readConfigFile('global.json');
17
- return lowdefyGlobal || {
18
- };
17
+ return lowdefyGlobal || {};
19
18
  }
20
19
  export default getLowdefyGlobal;
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -12,19 +12,15 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import findHome from './findHome.js';
15
+ */ import getHomeAndMenus from './getHomeAndMenus.js';
16
16
  import getLowdefyGlobal from './getLowdefyGlobal.js';
17
- import getMenus from './menus/getMenus.js';
18
17
  async function getRootConfig(context) {
19
- const [lowdefyGlobal, menus] = await Promise.all([
18
+ const [lowdefyGlobal, { home , menus }] = await Promise.all([
20
19
  getLowdefyGlobal(context),
21
- getMenus(context)
20
+ getHomeAndMenus(context),
22
21
  ]);
23
22
  return {
24
- authenticated: context.authenticated,
25
- home: findHome(context, {
26
- menus
27
- }),
23
+ home,
28
24
  lowdefyGlobal,
29
25
  menus
30
26
  };
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { get } from '@lowdefy/helpers';
16
- import filterMenuList from './filterMenuList';
16
+ import filterMenuList from './filterMenuList.js';
17
17
  function filterMenus(context, { menus }) {
18
18
  return menus.map((menu)=>{
19
19
  return {
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import filterMenus from './filterMenus';
15
+ */ import filterMenus from './filterMenus.js';
16
16
  async function getMenus(context) {
17
17
  const unfilteredMenus = await context.readConfigFile('menus.json');
18
18
  return filterMenus(context, {
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -13,38 +13,29 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createAuthorize from '../context/createAuthorize.js';
16
- function testContext({ config ={
17
- } , connections ={
18
- } , headers ={
19
- } , host ='host' , logger ={
20
- debug: ()=>{
21
- },
22
- error: ()=>{
23
- },
24
- info: ()=>{
25
- },
26
- warn: ()=>{
27
- }
28
- } , readConfigFile , roles , secrets ={
29
- } , setHeader , user , protocol ='https' , } = {
30
- }) {
31
- const authenticated = user && !!user.sub;
16
+ function testContext({ config ={} , connections ={} , headers ={} , host ='host' , logger ={
17
+ debug: ()=>{},
18
+ error: ()=>{},
19
+ info: ()=>{},
20
+ warn: ()=>{}
21
+ } , operators ={
22
+ _test: ()=>'test'
23
+ } , readConfigFile , secrets ={} , setHeader , session , protocol ='https' , } = {}) {
32
24
  return {
33
- authenticated,
34
25
  authorize: createAuthorize({
35
- authenticated,
36
- roles
26
+ session
37
27
  }),
38
28
  config,
39
29
  connections,
40
30
  headers,
41
31
  host,
42
32
  logger,
33
+ operators,
43
34
  protocol,
44
35
  readConfigFile,
45
36
  secrets,
46
37
  setHeader,
47
- user
38
+ user: session?.user
48
39
  };
49
40
  }
50
41
  export default testContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-alpha.12",
4
4
  "licence": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -38,23 +38,25 @@
38
38
  "clean": "rm -rf dist",
39
39
  "prepare": "yarn build",
40
40
  "swc": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start",
41
- "test": "jest --coverage"
41
+ "test": "yarn node --experimental-vm-modules $(yarn bin jest)"
42
42
  },
43
43
  "dependencies": {
44
- "@lowdefy/ajv": "4.0.0-alpha.1",
45
- "@lowdefy/helpers": "4.0.0-alpha.1",
46
- "@lowdefy/node-utils": "4.0.0-alpha.1",
47
- "@lowdefy/nunjucks": "4.0.0-alpha.1",
48
- "@lowdefy/operators": "4.0.0-alpha.1"
44
+ "@lowdefy/ajv": "4.0.0-alpha.12",
45
+ "@lowdefy/helpers": "4.0.0-alpha.12",
46
+ "@lowdefy/node-utils": "4.0.0-alpha.12",
47
+ "@lowdefy/nunjucks": "4.0.0-alpha.12",
48
+ "@lowdefy/operators": "4.0.0-alpha.12"
49
49
  },
50
50
  "devDependencies": {
51
- "@swc/cli": "0.1.52",
52
- "@swc/core": "1.2.112",
53
- "@swc/jest": "0.2.9",
54
- "jest": "27.3.1"
51
+ "@jest/globals": "27.5.1",
52
+ "@lowdefy/operators-js": "4.0.0-alpha.12",
53
+ "@swc/cli": "0.1.55",
54
+ "@swc/core": "1.2.135",
55
+ "@swc/jest": "0.2.17",
56
+ "jest": "27.5.1"
55
57
  },
56
58
  "publishConfig": {
57
59
  "access": "public"
58
60
  },
59
- "gitHead": "c97a8fa6b5a641e7d50df09f5601a9c586eeb65a"
61
+ "gitHead": "41b6138a81bee7da362dad06345bc9f87b2c2133"
60
62
  }
@@ -1,46 +0,0 @@
1
- /*
2
- Copyright 2020-2021 Lowdefy, Inc
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */ import createAuthorize from './createAuthorize.js';
16
- import createReadConfigFile from './readConfigFile.js';
17
- import verifyAuthorizationHeader from './verifyAuthorizationHeader.js';
18
- async function createContext({ buildDirectory , connections , secrets }) {
19
- const readConfigFile = createReadConfigFile({
20
- buildDirectory
21
- });
22
- const config = await readConfigFile('config.json');
23
- function contextFn({ headers , host , logger , protocol , setHeader }) {
24
- const context = {
25
- config,
26
- connections,
27
- headers,
28
- host,
29
- logger,
30
- protocol,
31
- readConfigFile,
32
- secrets,
33
- setHeader
34
- };
35
- const { authenticated , user , roles } = verifyAuthorizationHeader(context);
36
- context.authorize = createAuthorize({
37
- authenticated,
38
- roles
39
- });
40
- context.authenticated = authenticated;
41
- context.user = user;
42
- return context;
43
- }
44
- return contextFn;
45
- }
46
- export default createContext;