-
Notifications
You must be signed in to change notification settings - Fork 0
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 #3 from kontent-ai/KCL-12377_fix_getting_preview_a…
…pi_key KCL-12377 Fix code that gets preview api key for a project
- Loading branch information
Showing
6 changed files
with
68 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import { IAppContext } from "../context/AppContext"; | ||
import { IAuthContext } from "../context/AuthContext"; | ||
import { IPreviewApiKey } from "../repositories/previewApiKeyRepository"; | ||
import { getPreviewApiTokenSeed, getKeyForTokenSeed } from "../repositories/previewApiKeyRepository"; | ||
import { getProjectContainerForEnvironment } from "../repositories/projectContainerRepository"; | ||
|
||
interface ILoadPreviewApiKeyDeps { | ||
readonly appContext: IAppContext; | ||
readonly authContext: IAuthContext; | ||
readonly getPreviewApiKey: (authToken: string, projectId: string) => Promise<IPreviewApiKey>; | ||
} | ||
|
||
export const createLoadPreviewApiKey = (props: ILoadPreviewApiKeyDeps): () => Promise<string | null> => { | ||
const { accessToken } = props.authContext; | ||
const { projectId } = props.appContext; | ||
return () => props.getPreviewApiKey(accessToken, projectId) | ||
.then((response: IPreviewApiKey) => { | ||
return response.api_key; | ||
}) | ||
.catch(() => { | ||
return async () => { | ||
const projectContainerId = await getProjectContainerForEnvironment(accessToken, projectId).then(res => res.projectContainerId); | ||
const tokenSeed = await getPreviewApiTokenSeed(accessToken, projectContainerId, projectId).then(res => res[0]?.token_seed_id); | ||
|
||
if (!tokenSeed) { | ||
return null; | ||
}) | ||
} | ||
|
||
return getKeyForTokenSeed(accessToken, projectContainerId, tokenSeed) | ||
.then(response => response.api_key) | ||
.catch(() => null); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createAjaxWithCredentials } from '../utils/ajax'; | ||
import { | ||
createRestProvider, | ||
IRequestContext, | ||
} from '../utils/restProvider'; | ||
|
||
const restProvider = createRestProvider(createAjaxWithCredentials()); | ||
|
||
export interface ProjectContainer { | ||
projectContainerId: string; | ||
} | ||
|
||
export const getProjectContainerForEnvironment = (authToken: string, environmentId: string): Promise<ProjectContainer> => { | ||
const requestContext: IRequestContext = { | ||
authToken: authToken, | ||
}; | ||
const url = `${process.env.REACT_APP_KONTENT_URL}/api/project-management/${environmentId}`; | ||
return restProvider.get(url, requestContext); | ||
}; |
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