This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
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.
Closes #12
- Loading branch information
James Canning
committed
Jan 23, 2018
1 parent
3c4368e
commit 9c0df37
Showing
10 changed files
with
290 additions
and
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import fetch from 'node-fetch'; | ||
import { ContentApiOptions } from './types'; | ||
import chalk from 'chalk'; | ||
|
||
export function normaliseContentPath(path: string) { | ||
if (path === '/') { | ||
return path; | ||
} | ||
|
||
let fixedPath = path.endsWith('/') ? path.slice(0, -1) : path; | ||
fixedPath = !fixedPath.startsWith('/') ? `/${fixedPath}` : fixedPath; | ||
|
||
return fixedPath; | ||
} | ||
|
||
async function loadContentPathsFromApi(contentApiOptions: ContentApiOptions) { | ||
const response = await fetch(contentApiOptions.endpoint, { | ||
method: 'post', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: `query AllContentPages { | ||
allPages { | ||
path | ||
} | ||
}`, | ||
}), | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
console.log( | ||
chalk`{keyword('orange') [content] {bold ${ | ||
data.data.allPages.length | ||
}} page paths loaded from content api}` | ||
); | ||
|
||
return data.data.allPages | ||
.map((page: { path: string | null }) => { | ||
if ( | ||
!page.path || | ||
contentApiOptions.skipPaths.indexOf(page.path) >= 0 | ||
) { | ||
return null; | ||
} | ||
|
||
return normaliseContentPath(page.path); | ||
}) | ||
.filter((page: { path: string | null }) => page !== null); | ||
} | ||
|
||
export function createContentCache() { | ||
const cache = { paths: null, data: null }; | ||
console.log(chalk`{keyword('orange') [content] content api enabled.}`); | ||
|
||
return { | ||
async getAllPaths(contentApiOptions: ContentApiOptions) { | ||
if (cache.paths !== null) { | ||
return cache.paths; | ||
} | ||
|
||
const paths = await loadContentPathsFromApi(contentApiOptions); | ||
cache.paths = paths; | ||
|
||
return paths; | ||
}, | ||
}; | ||
} |
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
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
Oops, something went wrong.