@intra-mart/smartlime 0.0.1-beta
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/.gitattributes +1 -0
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/lib/_shared/renderTarget.d.ts +14 -0
- package/lib/_shared/renderTarget.js +31 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.js +18 -0
- package/lib/packages/Fetch/IMFecthError.d.ts +4 -0
- package/lib/packages/Fetch/IMFecthError.js +10 -0
- package/lib/packages/Fetch/index.d.ts +14 -0
- package/lib/packages/Fetch/index.js +112 -0
- package/lib/packages/Me/Context.d.ts +10 -0
- package/lib/packages/Me/Context.js +2 -0
- package/lib/packages/Me/IMMeError.d.ts +4 -0
- package/lib/packages/Me/IMMeError.js +10 -0
- package/lib/packages/Me/hooks/index.d.ts +2 -0
- package/lib/packages/Me/hooks/index.js +2 -0
- package/lib/packages/Me/hooks/useMe.d.ts +6 -0
- package/lib/packages/Me/hooks/useMe.js +11 -0
- package/lib/packages/Me/hooks/useMeState.d.ts +2 -0
- package/lib/packages/Me/hooks/useMeState.js +21 -0
- package/lib/packages/Me/index.d.ts +22 -0
- package/lib/packages/Me/index.js +55 -0
- package/lib/packages/Me/type.d.ts +85 -0
- package/lib/packages/Me/type.js +1 -0
- package/lib/packages/OAuth/Context.d.ts +14 -0
- package/lib/packages/OAuth/Context.js +2 -0
- package/lib/packages/OAuth/IMOAuthError.d.ts +4 -0
- package/lib/packages/OAuth/IMOAuthError.js +10 -0
- package/lib/packages/OAuth/hooks/index.d.ts +5 -0
- package/lib/packages/OAuth/hooks/index.js +5 -0
- package/lib/packages/OAuth/hooks/useAuthState.d.ts +2 -0
- package/lib/packages/OAuth/hooks/useAuthState.js +25 -0
- package/lib/packages/OAuth/hooks/useAuthStateEffect.d.ts +6 -0
- package/lib/packages/OAuth/hooks/useAuthStateEffect.js +14 -0
- package/lib/packages/OAuth/hooks/useIMOAuth.d.ts +7 -0
- package/lib/packages/OAuth/hooks/useIMOAuth.js +13 -0
- package/lib/packages/OAuth/hooks/useIMToken.d.ts +2 -0
- package/lib/packages/OAuth/hooks/useIMToken.js +21 -0
- package/lib/packages/OAuth/hooks/useStartAuth.d.ts +2 -0
- package/lib/packages/OAuth/hooks/useStartAuth.js +12 -0
- package/lib/packages/OAuth/index.d.ts +39 -0
- package/lib/packages/OAuth/index.js +299 -0
- package/lib/packages/Search/Context.d.ts +6 -0
- package/lib/packages/Search/Context.js +2 -0
- package/lib/packages/Search/IMSearchError.d.ts +4 -0
- package/lib/packages/Search/IMSearchError.js +10 -0
- package/lib/packages/Search/hooks/index.d.ts +11 -0
- package/lib/packages/Search/hooks/index.js +11 -0
- package/lib/packages/Search/hooks/useDepartmentPostSearch.d.ts +2 -0
- package/lib/packages/Search/hooks/useDepartmentPostSearch.js +48 -0
- package/lib/packages/Search/hooks/useDepartmentSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/useDepartmentSerch.js +56 -0
- package/lib/packages/Search/hooks/useDepartmentTopSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/useDepartmentTopSerch.js +47 -0
- package/lib/packages/Search/hooks/useIMSearch.d.ts +4 -0
- package/lib/packages/Search/hooks/useIMSearch.js +11 -0
- package/lib/packages/Search/hooks/usePostSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/usePostSerch.js +48 -0
- package/lib/packages/Search/hooks/usePublicGroupRoleSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/usePublicGroupRoleSerch.js +50 -0
- package/lib/packages/Search/hooks/usePublicGroupSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/usePublicGroupSerch.js +56 -0
- package/lib/packages/Search/hooks/usePublicGroupTopSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/usePublicGroupTopSerch.js +47 -0
- package/lib/packages/Search/hooks/useRoleSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/useRoleSerch.js +50 -0
- package/lib/packages/Search/hooks/useUserAdditionalInformationSearch.d.ts +2 -0
- package/lib/packages/Search/hooks/useUserAdditionalInformationSearch.js +50 -0
- package/lib/packages/Search/hooks/useUserSerch.d.ts +2 -0
- package/lib/packages/Search/hooks/useUserSerch.js +68 -0
- package/lib/packages/Search/hooks/utils.d.ts +4 -0
- package/lib/packages/Search/hooks/utils.js +25 -0
- package/lib/packages/Search/index.d.ts +49 -0
- package/lib/packages/Search/index.js +72 -0
- package/lib/packages/Search/types.d.ts +208 -0
- package/lib/packages/Search/types.js +1 -0
- package/lib/packages/Session/Context.d.ts +16 -0
- package/lib/packages/Session/Context.js +2 -0
- package/lib/packages/Session/IMSessionError.d.ts +4 -0
- package/lib/packages/Session/IMSessionError.js +10 -0
- package/lib/packages/Session/hooks/index.d.ts +2 -0
- package/lib/packages/Session/hooks/index.js +2 -0
- package/lib/packages/Session/hooks/useSession.d.ts +12 -0
- package/lib/packages/Session/hooks/useSession.js +11 -0
- package/lib/packages/Session/hooks/useSessionState.d.ts +4 -0
- package/lib/packages/Session/hooks/useSessionState.js +24 -0
- package/lib/packages/Session/index.d.ts +16 -0
- package/lib/packages/Session/index.js +164 -0
- package/lib/packages/Session/type.d.ts +5 -0
- package/lib/packages/Session/type.js +1 -0
- package/lib/packages/Session/utils.d.ts +2 -0
- package/lib/packages/Session/utils.js +6 -0
- package/lib/packages/Smartlime/NotSearchPropError.d.ts +2 -0
- package/lib/packages/Smartlime/NotSearchPropError.js +13 -0
- package/lib/packages/Smartlime/index.d.ts +19 -0
- package/lib/packages/Smartlime/index.js +23 -0
- package/lib/packages/Tenant/Context.d.ts +6 -0
- package/lib/packages/Tenant/Context.js +2 -0
- package/lib/packages/Tenant/IMTenantError.d.ts +4 -0
- package/lib/packages/Tenant/IMTenantError.js +10 -0
- package/lib/packages/Tenant/hooks/index.d.ts +1 -0
- package/lib/packages/Tenant/hooks/index.js +1 -0
- package/lib/packages/Tenant/hooks/useIMBaseUrl.d.ts +2 -0
- package/lib/packages/Tenant/hooks/useIMBaseUrl.js +10 -0
- package/lib/packages/Tenant/index.d.ts +10 -0
- package/lib/packages/Tenant/index.js +19 -0
- package/lib/packages/WebView/IMWebViewError.d.ts +4 -0
- package/lib/packages/WebView/IMWebViewError.js +10 -0
- package/lib/packages/WebView/hooks/index.d.ts +1 -0
- package/lib/packages/WebView/hooks/index.js +1 -0
- package/lib/packages/WebView/hooks/useHackSearchUI.d.ts +8 -0
- package/lib/packages/WebView/hooks/useHackSearchUI.js +49 -0
- package/lib/packages/WebView/index.d.ts +11 -0
- package/lib/packages/WebView/index.js +166 -0
- package/lib/utils/mergeRef.d.ts +1 -0
- package/lib/utils/mergeRef.js +14 -0
- package/lib/utils/path.d.ts +1 -0
- package/lib/utils/path.js +16 -0
- package/package.json +87 -0
- package/tsconfig.json +36 -0
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* text=auto eol=crlf
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 accelplatform
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type ForceRender = () => void;
|
|
2
|
+
export declare const createRenderTarget: () => {
|
|
3
|
+
addEventListener: (callback: ForceRender) => void;
|
|
4
|
+
removeEventListener: (callback: ForceRender) => void;
|
|
5
|
+
dispatch: () => void;
|
|
6
|
+
};
|
|
7
|
+
export type CreateRenderTarget = typeof createRenderTarget;
|
|
8
|
+
export type RenderTarget = ReturnType<CreateRenderTarget>;
|
|
9
|
+
export declare const useRenderTarget: () => {
|
|
10
|
+
addEventListener: (callback: ForceRender) => void;
|
|
11
|
+
removeEventListener: (callback: ForceRender) => void;
|
|
12
|
+
dispatch: () => void;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
export const createRenderTarget = () => {
|
|
3
|
+
const listenerList = [];
|
|
4
|
+
const addEventListener = (callback) => {
|
|
5
|
+
listenerList.push(callback);
|
|
6
|
+
};
|
|
7
|
+
const removeEventListener = (callback) => {
|
|
8
|
+
for (let i = 0, l = listenerList.length; i < l; i++) {
|
|
9
|
+
if (listenerList[i] === callback) {
|
|
10
|
+
listenerList.splice(i, 1);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const dispatch = () => {
|
|
16
|
+
for (let i = 0, l = listenerList.length; i < l; i++) {
|
|
17
|
+
const listener = listenerList[i];
|
|
18
|
+
if (listener)
|
|
19
|
+
listener();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
addEventListener,
|
|
24
|
+
removeEventListener,
|
|
25
|
+
dispatch,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const useRenderTarget = () => {
|
|
29
|
+
const ref = useRef(createRenderTarget());
|
|
30
|
+
return ref.current;
|
|
31
|
+
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { Smartlime } from './packages/Smartlime';
|
|
2
|
+
export { IMTenant } from './packages/Tenant';
|
|
3
|
+
export * from './packages/Tenant/hooks';
|
|
4
|
+
export { IMOAuth } from './packages/OAuth';
|
|
5
|
+
export * from './packages/OAuth/hooks';
|
|
6
|
+
export { useIMFetch } from './packages/Fetch';
|
|
7
|
+
export { IMMe } from './packages/Me';
|
|
8
|
+
export * from './packages/Me/hooks';
|
|
9
|
+
export { IMSession } from './packages/Session';
|
|
10
|
+
export * from './packages/Session/hooks';
|
|
11
|
+
export { IMSearch } from './packages/Search';
|
|
12
|
+
export * from './packages/Search/hooks';
|
|
13
|
+
export { IMWebView } from './packages/WebView';
|
|
14
|
+
export * from './packages/WebView/hooks';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
TODO
|
|
3
|
+
consider separating the entry points in "exports" in package.json.
|
|
4
|
+
*/
|
|
5
|
+
export { Smartlime } from './packages/Smartlime';
|
|
6
|
+
export { IMTenant } from './packages/Tenant';
|
|
7
|
+
export * from './packages/Tenant/hooks';
|
|
8
|
+
export { IMOAuth } from './packages/OAuth';
|
|
9
|
+
export * from './packages/OAuth/hooks';
|
|
10
|
+
export { useIMFetch } from './packages/Fetch';
|
|
11
|
+
export { IMMe } from './packages/Me';
|
|
12
|
+
export * from './packages/Me/hooks';
|
|
13
|
+
export { IMSession } from './packages/Session';
|
|
14
|
+
export * from './packages/Session/hooks';
|
|
15
|
+
export { IMSearch } from './packages/Search';
|
|
16
|
+
export * from './packages/Search/hooks';
|
|
17
|
+
export { IMWebView } from './packages/WebView';
|
|
18
|
+
export * from './packages/WebView/hooks';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const errorType = 'IMFecthError';
|
|
2
|
+
export class IMFecthError extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
if (Error.captureStackTrace) {
|
|
6
|
+
Error.captureStackTrace(this, IMFecthError);
|
|
7
|
+
}
|
|
8
|
+
this.name = errorType;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Context as DefaultContext } from '../OAuth/Context';
|
|
2
|
+
export type IMJson = {
|
|
3
|
+
error?: boolean;
|
|
4
|
+
errorMessage?: string;
|
|
5
|
+
data?: Record<string, unknown>;
|
|
6
|
+
};
|
|
7
|
+
interface IMResponse<T> extends Response {
|
|
8
|
+
json: () => Promise<T extends IMJson ? T & IMJson : undefined extends T ? IMJson : T>;
|
|
9
|
+
}
|
|
10
|
+
type UseIMFetch = (option?: {
|
|
11
|
+
noValidate?: boolean;
|
|
12
|
+
}, context?: typeof DefaultContext) => <T>(input: string, init?: RequestInit) => Promise<IMResponse<T>>;
|
|
13
|
+
export declare const useIMFetch: UseIMFetch;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useCallback, useContext } from 'react';
|
|
2
|
+
import { useIMBaseUrl } from '../..';
|
|
3
|
+
import { join } from '../../utils/path';
|
|
4
|
+
import { Context as DefaultContext } from '../OAuth/Context';
|
|
5
|
+
import { IMOAuthError } from '../OAuth/IMOAuthError';
|
|
6
|
+
import { IMFecthError } from './IMFecthError';
|
|
7
|
+
const fetchWrapper = async (token, input, init = {}) => {
|
|
8
|
+
const response = await fetch(input, {
|
|
9
|
+
credentials: 'omit',
|
|
10
|
+
...init,
|
|
11
|
+
headers: {
|
|
12
|
+
'X-Intramart-Session': 'never',
|
|
13
|
+
...init.headers,
|
|
14
|
+
Authorization: `Bearer ${token}`,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
return response;
|
|
18
|
+
};
|
|
19
|
+
const correctionAuthMethod = (input) => {
|
|
20
|
+
if (input.startsWith('/api/bearer')) {
|
|
21
|
+
if (input.startsWith('/api/bearer/workflow'))
|
|
22
|
+
throw new IMFecthError('api/bearer is not available in the workflow API, api/oauth should be used.');
|
|
23
|
+
return input;
|
|
24
|
+
}
|
|
25
|
+
if (input.startsWith('/api/oauth')) {
|
|
26
|
+
if (input.startsWith('/api/oauth/workflow') ||
|
|
27
|
+
input.startsWith('/api/oauth/immaster'))
|
|
28
|
+
return input;
|
|
29
|
+
throw new IMFecthError('api/oauth is not available in the API except for workflow or immaster, please use api/bearer.');
|
|
30
|
+
}
|
|
31
|
+
if (input.startsWith('/api/workflow') || input.startsWith('/api/immaster'))
|
|
32
|
+
return input.replace(/^\/api\//, '/api/oauth/');
|
|
33
|
+
if (input.startsWith('/api/'))
|
|
34
|
+
return input.replace(/^\/api\//, '/api/bearer/');
|
|
35
|
+
return input;
|
|
36
|
+
};
|
|
37
|
+
const validateRelativePath = (input) => {
|
|
38
|
+
if (/^.+:\/\//.test(input))
|
|
39
|
+
return false;
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
const validateImApiPath = (input) => {
|
|
43
|
+
if (input.startsWith('/api'))
|
|
44
|
+
return true;
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
const correctionInput = (input, baseUrl, noValidate = false) => {
|
|
48
|
+
// 外部にトークンを送りたいケースが想定できないためnoValidateがtrueでも相対パスだけは強制する
|
|
49
|
+
const valid = validateRelativePath(input);
|
|
50
|
+
if (!valid)
|
|
51
|
+
throw new IMFecthError('input must be a relative path');
|
|
52
|
+
const urlWithCorrectedSlash = `${input.startsWith('/') ? '' : '/'}${input}`;
|
|
53
|
+
if (!noValidate) {
|
|
54
|
+
if (validateImApiPath(urlWithCorrectedSlash)) {
|
|
55
|
+
const correctedUrl = correctionAuthMethod(urlWithCorrectedSlash);
|
|
56
|
+
return join(baseUrl, correctedUrl);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return join(baseUrl, urlWithCorrectedSlash);
|
|
60
|
+
};
|
|
61
|
+
const checkUnauthorizedResponse = async (response) => {
|
|
62
|
+
if (response.status === 401) {
|
|
63
|
+
try {
|
|
64
|
+
const clone = response.clone();
|
|
65
|
+
const json = await clone.json();
|
|
66
|
+
if (json.error === 'invalid_token')
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
};
|
|
75
|
+
export const useIMFetch = (option = { noValidate: false }, context) => {
|
|
76
|
+
const oauthContext = useContext(context || DefaultContext);
|
|
77
|
+
const baseUrl = useIMBaseUrl();
|
|
78
|
+
if (oauthContext == null) {
|
|
79
|
+
throw new IMFecthError('useIMToken requires either a Context provide or an ancestor element with a IMOAuthProvider.');
|
|
80
|
+
}
|
|
81
|
+
return useCallback(async (input, init = {}) => {
|
|
82
|
+
const token = oauthContext.getToken();
|
|
83
|
+
if (!token) {
|
|
84
|
+
throw new IMFecthError(`not yet authorized`);
|
|
85
|
+
}
|
|
86
|
+
const correctedInput = correctionInput(input, baseUrl, option.noValidate);
|
|
87
|
+
try {
|
|
88
|
+
const response = await fetchWrapper(token, correctedInput, init);
|
|
89
|
+
if (await checkUnauthorizedResponse(response)) {
|
|
90
|
+
await oauthContext.refresh();
|
|
91
|
+
const tokenAfterRefresh = oauthContext.getToken();
|
|
92
|
+
if (tokenAfterRefresh != null) {
|
|
93
|
+
const reFetchResponse = await fetchWrapper(tokenAfterRefresh, correctedInput, init);
|
|
94
|
+
if (await checkUnauthorizedResponse(reFetchResponse)) {
|
|
95
|
+
throw new IMFecthError('token is invalid.');
|
|
96
|
+
}
|
|
97
|
+
return reFetchResponse;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return response;
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
if (e instanceof IMOAuthError || e instanceof IMFecthError) {
|
|
104
|
+
oauthContext.destroy();
|
|
105
|
+
throw new IMFecthError('token has been destroyed.', {
|
|
106
|
+
cause: e,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
111
|
+
}, [oauthContext, baseUrl]);
|
|
112
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { ErrorResult, SuccessResult } from '.';
|
|
3
|
+
import { RenderTarget } from '../../_shared/renderTarget';
|
|
4
|
+
import { Me } from './type';
|
|
5
|
+
export interface MeContext {
|
|
6
|
+
getMe: () => Me | null;
|
|
7
|
+
request: () => Promise<SuccessResult | ErrorResult>;
|
|
8
|
+
meStateRenderTarget: RenderTarget;
|
|
9
|
+
}
|
|
10
|
+
export declare const Context: import("react").Context<MeContext | null>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Context as DefaultContext } from '../Context';
|
|
2
|
+
export declare const useMe: (Context?: typeof DefaultContext) => {
|
|
3
|
+
getMe: () => import("../type").Me | null;
|
|
4
|
+
request: () => Promise<import("..").SuccessResult | import("..").ErrorResult>;
|
|
5
|
+
update: () => Promise<import("..").SuccessResult | import("..").ErrorResult>;
|
|
6
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { Context as DefaultContext } from '../Context';
|
|
3
|
+
import { IMMeError } from '../IMMeError';
|
|
4
|
+
export const useMe = (Context) => {
|
|
5
|
+
const meContext = useContext(Context || DefaultContext);
|
|
6
|
+
if (meContext == null) {
|
|
7
|
+
throw new IMMeError('useMe requires either a Context provide or an ancestor element with a IMMeProvider.');
|
|
8
|
+
}
|
|
9
|
+
const { getMe, request } = meContext;
|
|
10
|
+
return { getMe, request, update: request };
|
|
11
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useContext, useEffect, useReducer, useRef } from 'react';
|
|
2
|
+
import { Context as DefaultContext } from '../Context';
|
|
3
|
+
import { IMMeError } from '../IMMeError';
|
|
4
|
+
export const useMeState = (Context) => {
|
|
5
|
+
const initRef = useRef(true);
|
|
6
|
+
const [, forceRender] = useReducer((s) => s + 1, 0);
|
|
7
|
+
const meContext = useContext(Context || DefaultContext);
|
|
8
|
+
if (meContext == null) {
|
|
9
|
+
throw new IMMeError('useMeState requires either a Context provide or an ancestor element with a IMMeProvider.');
|
|
10
|
+
}
|
|
11
|
+
if (initRef.current) {
|
|
12
|
+
initRef.current = false;
|
|
13
|
+
meContext.meStateRenderTarget.addEventListener(forceRender);
|
|
14
|
+
}
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
return () => {
|
|
17
|
+
meContext.meStateRenderTarget.removeEventListener(forceRender);
|
|
18
|
+
};
|
|
19
|
+
}, []);
|
|
20
|
+
return meContext.getMe();
|
|
21
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { RenderTarget } from '../../_shared/renderTarget';
|
|
3
|
+
import { Context as DefaultContext } from './Context';
|
|
4
|
+
import { Me } from './type';
|
|
5
|
+
export interface MeResponse {
|
|
6
|
+
error: boolean;
|
|
7
|
+
data: Me;
|
|
8
|
+
}
|
|
9
|
+
export interface SuccessResult {
|
|
10
|
+
status: 'success';
|
|
11
|
+
}
|
|
12
|
+
export interface ErrorResult {
|
|
13
|
+
status: 'error';
|
|
14
|
+
}
|
|
15
|
+
export declare const useRequestIMMe: (setter: ReturnType<typeof useMeStateRef>[1]) => () => Promise<SuccessResult | ErrorResult>;
|
|
16
|
+
declare const useMeStateRef: (renderTarget: RenderTarget) => readonly [import("react").MutableRefObject<Me | null>, (state: Me, silent?: boolean) => void, () => Me | null];
|
|
17
|
+
export interface IMMeProps {
|
|
18
|
+
children: JSX.Element;
|
|
19
|
+
meContext?: typeof DefaultContext;
|
|
20
|
+
}
|
|
21
|
+
export declare const IMMe: ({ children, meContext }: IMMeProps) => JSX.Element;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useCallback, useRef } from 'react';
|
|
2
|
+
import { useRenderTarget } from '../../_shared/renderTarget';
|
|
3
|
+
import { useIMFetch } from '../Fetch';
|
|
4
|
+
import { Context as DefaultContext } from './Context';
|
|
5
|
+
import { IMMeError } from './IMMeError';
|
|
6
|
+
export const useRequestIMMe = (setter) => {
|
|
7
|
+
const imFetch = useIMFetch();
|
|
8
|
+
const request = useCallback(async () => {
|
|
9
|
+
try {
|
|
10
|
+
const response = await imFetch('api/bearer/smacolow/me', {
|
|
11
|
+
headers: {
|
|
12
|
+
Accept: 'application/json',
|
|
13
|
+
},
|
|
14
|
+
method: 'GET',
|
|
15
|
+
});
|
|
16
|
+
if (response.status === 200) {
|
|
17
|
+
const json = await response.json();
|
|
18
|
+
if (!json.error) {
|
|
19
|
+
setter(json.data);
|
|
20
|
+
return { status: 'success' };
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw new IMMeError('failed to get me information.');
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return { status: 'error' };
|
|
27
|
+
}
|
|
28
|
+
}, [imFetch]);
|
|
29
|
+
return request;
|
|
30
|
+
};
|
|
31
|
+
const useMeStateRef = (renderTarget) => {
|
|
32
|
+
const ref = useRef(null);
|
|
33
|
+
const setter = useCallback((state, silent) => {
|
|
34
|
+
if (ref.current === state)
|
|
35
|
+
return;
|
|
36
|
+
ref.current = state;
|
|
37
|
+
if (!silent)
|
|
38
|
+
renderTarget.dispatch();
|
|
39
|
+
}, []);
|
|
40
|
+
const getter = useCallback(() => ref.current, []);
|
|
41
|
+
return [ref, setter, getter];
|
|
42
|
+
};
|
|
43
|
+
export const IMMe = ({ children, meContext }) => {
|
|
44
|
+
const Context = meContext || DefaultContext;
|
|
45
|
+
const contextRef = useRef();
|
|
46
|
+
const meStateRenderTarget = useRenderTarget();
|
|
47
|
+
const [, setMe, getMe] = useMeStateRef(meStateRenderTarget);
|
|
48
|
+
const request = useRequestIMMe(setMe);
|
|
49
|
+
contextRef.current = {
|
|
50
|
+
getMe,
|
|
51
|
+
request,
|
|
52
|
+
meStateRenderTarget,
|
|
53
|
+
};
|
|
54
|
+
return (<Context.Provider value={contextRef.current}>{children}</Context.Provider>);
|
|
55
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export interface AllDepartment {
|
|
2
|
+
companyCd: string;
|
|
3
|
+
departmentCd: string;
|
|
4
|
+
departmentFullName: string;
|
|
5
|
+
departmentName: string;
|
|
6
|
+
departmentSearchName: string;
|
|
7
|
+
departmentSetCd: string;
|
|
8
|
+
departmentShortName: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Company {
|
|
11
|
+
companyCd: string;
|
|
12
|
+
companyName: string;
|
|
13
|
+
companyShortName: string;
|
|
14
|
+
companySearchName: string;
|
|
15
|
+
}
|
|
16
|
+
export interface Post {
|
|
17
|
+
companyCd: string;
|
|
18
|
+
departmentCd: string;
|
|
19
|
+
departmentFullName: string;
|
|
20
|
+
departmentName: string;
|
|
21
|
+
departmentSearchName: string;
|
|
22
|
+
departmentSetCd: string;
|
|
23
|
+
departmentShortName: string;
|
|
24
|
+
postCd: string;
|
|
25
|
+
postName: string;
|
|
26
|
+
rank: number;
|
|
27
|
+
}
|
|
28
|
+
export interface PublicGroup {
|
|
29
|
+
publicGroupCd: string;
|
|
30
|
+
publicGroupFullName: string;
|
|
31
|
+
publicGroupName: string;
|
|
32
|
+
publicGroupSearchName: string;
|
|
33
|
+
publicGroupSetCd: string;
|
|
34
|
+
publicGroupShortName: string;
|
|
35
|
+
}
|
|
36
|
+
export interface PublicGroupRole {
|
|
37
|
+
publicGroupCd: string;
|
|
38
|
+
publicGroupFullName: string;
|
|
39
|
+
publicGroupName: string;
|
|
40
|
+
publicGroupSearchName: string;
|
|
41
|
+
publicGroupSetCd: string;
|
|
42
|
+
publicGroupShortName: string;
|
|
43
|
+
rank: number;
|
|
44
|
+
roleCd: string;
|
|
45
|
+
roleName: string;
|
|
46
|
+
}
|
|
47
|
+
export interface UserCategory {
|
|
48
|
+
categoryCd: string;
|
|
49
|
+
categoryItemCd: string;
|
|
50
|
+
categoryItemName: string;
|
|
51
|
+
categoryName: string;
|
|
52
|
+
}
|
|
53
|
+
export interface Me {
|
|
54
|
+
calendarId: string;
|
|
55
|
+
firstDayOfWeek: 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
56
|
+
locale: string;
|
|
57
|
+
tenantId: string;
|
|
58
|
+
timeZone: string;
|
|
59
|
+
userCd: string;
|
|
60
|
+
allDepartments: AllDepartment[];
|
|
61
|
+
allPosts: Post[];
|
|
62
|
+
companyList: Company[];
|
|
63
|
+
publicGroupList: PublicGroup[];
|
|
64
|
+
publicGroupRoleList: PublicGroupRole[];
|
|
65
|
+
userCategoryList: UserCategory[];
|
|
66
|
+
address1: string;
|
|
67
|
+
address2: string;
|
|
68
|
+
address3: string;
|
|
69
|
+
countryCd: string;
|
|
70
|
+
emailAddress1: string;
|
|
71
|
+
emailAddress2: string;
|
|
72
|
+
extensionFaxNumber: string;
|
|
73
|
+
extensionNumber: string;
|
|
74
|
+
faxNumber: string;
|
|
75
|
+
mobileEmailAddress: string;
|
|
76
|
+
mobileNumber: string;
|
|
77
|
+
notes: string;
|
|
78
|
+
sex: string;
|
|
79
|
+
telephoneNumber: string;
|
|
80
|
+
url: string;
|
|
81
|
+
userName: string;
|
|
82
|
+
userSearchName: string;
|
|
83
|
+
zipCode: string;
|
|
84
|
+
profileImage: string;
|
|
85
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { AuthState, StartAuthResult } from '.';
|
|
3
|
+
import { RenderTarget } from '../../_shared/renderTarget';
|
|
4
|
+
export interface OAuthContext {
|
|
5
|
+
getAuthState: () => AuthState;
|
|
6
|
+
getToken: () => string | null;
|
|
7
|
+
getTokenAsync: () => Promise<string | null>;
|
|
8
|
+
refresh: () => Promise<void>;
|
|
9
|
+
destroy: () => Promise<void>;
|
|
10
|
+
startAuth: () => Promise<StartAuthResult>;
|
|
11
|
+
tokenRenderTarget: RenderTarget;
|
|
12
|
+
authStateRenderTarget: RenderTarget;
|
|
13
|
+
}
|
|
14
|
+
export declare const Context: import("react").Context<OAuthContext | null>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const errorType = 'IMOAuthError';
|
|
2
|
+
export class IMOAuthError extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
if (Error.captureStackTrace) {
|
|
6
|
+
Error.captureStackTrace(this, IMOAuthError);
|
|
7
|
+
}
|
|
8
|
+
this.name = errorType;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useContext, useEffect, useReducer, useRef } from 'react';
|
|
2
|
+
import { Context as DefaultContext } from '../Context';
|
|
3
|
+
import { IMOAuthError } from '../IMOAuthError';
|
|
4
|
+
export const useAuthState = (Context) => {
|
|
5
|
+
const initRef = useRef(true);
|
|
6
|
+
const [, forceRender] = useReducer((s) => s + 1, 0);
|
|
7
|
+
const oauthContext = useContext(Context || DefaultContext);
|
|
8
|
+
if (oauthContext == null) {
|
|
9
|
+
throw new IMOAuthError('useAuthState requires either a Context provide or an ancestor element with a IMOAuthProvider.');
|
|
10
|
+
}
|
|
11
|
+
if (initRef.current) {
|
|
12
|
+
initRef.current = false;
|
|
13
|
+
oauthContext.authStateRenderTarget.addEventListener(forceRender);
|
|
14
|
+
}
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
return () => {
|
|
17
|
+
oauthContext.authStateRenderTarget.removeEventListener(forceRender);
|
|
18
|
+
};
|
|
19
|
+
}, []);
|
|
20
|
+
const state = oauthContext.getAuthState();
|
|
21
|
+
if (state == null) {
|
|
22
|
+
throw new IMOAuthError('auth status is null this is an exception that does not normally occur please create a new issue at https://github.com/accelplatform/smartlime/issues');
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AuthState } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* sugar syntax that sets the return value of "useAuthState" to the deps of "useEffect"
|
|
4
|
+
* this may be deprecated in the future
|
|
5
|
+
*/
|
|
6
|
+
export declare const useAuthStateEffect: (callBack: (state: AuthState) => ReturnType<React.EffectCallback>) => void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useAuthState } from '.';
|
|
3
|
+
/**
|
|
4
|
+
* sugar syntax that sets the return value of "useAuthState" to the deps of "useEffect"
|
|
5
|
+
* this may be deprecated in the future
|
|
6
|
+
*/
|
|
7
|
+
export const useAuthStateEffect = (callBack) => {
|
|
8
|
+
const state = useAuthState();
|
|
9
|
+
/*
|
|
10
|
+
* error because Destructor is using unique symbol
|
|
11
|
+
* use @ts-ignore to provide type safety for useAuthStateEffect users
|
|
12
|
+
@ts-ignore*/
|
|
13
|
+
useEffect(() => callBack(state), [state]);
|
|
14
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context as DefaultContext } from '../Context';
|
|
2
|
+
export declare const useIMOAuth: (Context?: typeof DefaultContext) => {
|
|
3
|
+
getToken: () => string | null;
|
|
4
|
+
getTokenAsync: () => Promise<string | null>;
|
|
5
|
+
refresh: () => Promise<void>;
|
|
6
|
+
destroy: () => Promise<void>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext, useMemo } from 'react';
|
|
2
|
+
import { Context as DefaultContext } from '../Context';
|
|
3
|
+
import { IMOAuthError } from '../IMOAuthError';
|
|
4
|
+
export const useIMOAuth = (Context) => {
|
|
5
|
+
const oauthContext = useContext(Context || DefaultContext);
|
|
6
|
+
if (oauthContext == null) {
|
|
7
|
+
throw new IMOAuthError('useIMOAuth requires either a Context provide or an ancestor element with a IMOAuthProvider.');
|
|
8
|
+
}
|
|
9
|
+
return useMemo(() => {
|
|
10
|
+
const { getToken, getTokenAsync, refresh, destroy } = oauthContext;
|
|
11
|
+
return { getToken, getTokenAsync, refresh, destroy };
|
|
12
|
+
}, [oauthContext]);
|
|
13
|
+
};
|