-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from kontent-ai/add_context_for_client
Add context for client
- Loading branch information
Showing
21 changed files
with
750 additions
and
684 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
REACT_APP_PROJECT_ID=your_project_id | ||
REACT_APP_ENVIRONMENT_ID=your_environment_id | ||
REACT_APP_PREVIEW_API_KEY=your_api_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import Cookies from 'universal-cookie'; | ||
import { | ||
camelCasePropertyNameResolver, | ||
DeliveryClient, | ||
} from '@kontent-ai/delivery-sdk'; | ||
import packageInfo from '../package.json'; | ||
import { selectedEnvironmentCookieName } from './const'; | ||
import validator from 'validator'; | ||
import { | ||
createContext, | ||
FC, | ||
PropsWithChildren, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
const sourceTrackingHeaderName = 'X-KC-SOURCE'; | ||
|
||
const previewApiKey = process.env.REACT_APP_PREVIEW_API_KEY || ''; | ||
|
||
const cookies = new Cookies(document.cookie); | ||
|
||
const getEnvironmentIdFromEnvironment = (): string | null | undefined => { | ||
const environmentIdFromEnv = process.env.REACT_APP_ENVIRONMENT_ID; | ||
|
||
if (environmentIdFromEnv && !validator.isUUID(environmentIdFromEnv)) { | ||
console.error( | ||
`Your environmentId (${environmentIdFromEnv}) given in your environment variables is not a valid GUID.` | ||
); | ||
return null; | ||
} | ||
|
||
return environmentIdFromEnv; | ||
}; | ||
|
||
const getEnvironmentIdFromCookies = (): string | null => { | ||
const environmentIdFromCookie = cookies.get(selectedEnvironmentCookieName); | ||
|
||
if (environmentIdFromCookie && !validator.isUUID(environmentIdFromCookie)) { | ||
console.error( | ||
`Your environmentId (${environmentIdFromCookie}) from cookies is not a valid GUID.` | ||
); | ||
return null; | ||
} | ||
|
||
return environmentIdFromCookie; | ||
}; | ||
|
||
const currentEnvironmentId = | ||
getEnvironmentIdFromEnvironment() ?? getEnvironmentIdFromCookies() ?? ''; | ||
|
||
const isPreview = (): boolean => previewApiKey !== ''; | ||
|
||
type GlobalHeadersType = { | ||
header: string; | ||
value: string; | ||
}; | ||
|
||
const createClient = (newEnvironmentId: string): DeliveryClient => | ||
new DeliveryClient({ | ||
environmentId: newEnvironmentId, | ||
previewApiKey: previewApiKey, | ||
defaultQueryConfig: { | ||
usePreviewMode: isPreview(), | ||
}, | ||
globalHeaders: (_queryConfig): GlobalHeadersType[] => [ | ||
{ | ||
header: sourceTrackingHeaderName, | ||
value: `${packageInfo.name};${packageInfo.version}`, | ||
}, | ||
], | ||
propertyNameResolver: camelCasePropertyNameResolver, | ||
}); | ||
|
||
const Client = createClient(currentEnvironmentId); | ||
|
||
type ClientState = [DeliveryClient, (environmentId: string) => void]; | ||
|
||
const ClientContext = createContext<ClientState>(undefined as never); | ||
|
||
export const useClient = (): ClientState => useContext(ClientContext); | ||
|
||
export const ClientProvider: FC = ({ | ||
children, | ||
}: PropsWithChildren<unknown>) => { | ||
const [client, setClient] = useState(Client); | ||
|
||
const updateClient = useCallback((newEnvironmentId: string) => { | ||
setClient(createClient(newEnvironmentId)); | ||
|
||
cookies.set(selectedEnvironmentCookieName, newEnvironmentId, { | ||
path: '/', | ||
sameSite: 'none', | ||
secure: true, | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<ClientContext.Provider | ||
value={useMemo(() => [client, updateClient], [client, updateClient])} | ||
> | ||
{children} | ||
</ClientContext.Provider> | ||
); | ||
}; | ||
|
||
export { | ||
createClient, | ||
getEnvironmentIdFromEnvironment, | ||
getEnvironmentIdFromCookies, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.