Skip to content

Commit

Permalink
Enable @typescript-eslint/no-use-before-define for examples dir
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Aug 10, 2022
1 parent 01888dc commit acea39b
Show file tree
Hide file tree
Showing 38 changed files with 450 additions and 440 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.json
Expand Up @@ -71,6 +71,7 @@
"functions": false,
"classes": false,
"variables": false,
"enums": false,
"typedefs": false
}
],
Expand Down Expand Up @@ -106,6 +107,16 @@
{
"files": ["examples/**/*"],
"rules": {
"@typescript-eslint/no-use-before-define": [
"error",
{
"functions": true,
"classes": true,
"variables": true,
"enums": true,
"typedefs": true
}
],
"import/no-anonymous-default-export": [
"error",
{
Expand Down
8 changes: 4 additions & 4 deletions examples/amp-first/components/amp/AmpCustomElement.tsx
Expand Up @@ -319,6 +319,10 @@ export function AmpIncludeAmpLinkRewriter() {
return <AmpIncludeCustomElement name="amp-link-rewriter" version="0.1" />
}

export function AmpIncludeAmpMustache() {
return <AmpIncludeCustomTemplate name="amp-mustache" version="0.2" />
}

export function AmpIncludeAmpList() {
return (
<>
Expand Down Expand Up @@ -350,10 +354,6 @@ export function AmpIncludeAmpMowplayer() {
return <AmpIncludeCustomElement name="amp-mowplayer" version="0.1" />
}

export function AmpIncludeAmpMustache() {
return <AmpIncludeCustomTemplate name="amp-mustache" version="0.2" />
}

export function AmpIncludeAmpNextPage() {
return <AmpIncludeCustomElement name="amp-next-page" version="0.1" />
}
Expand Down
14 changes: 7 additions & 7 deletions examples/amp-first/components/amp/AmpScript.tsx
Expand Up @@ -10,6 +10,13 @@ type AmpScriptProps = {
src?: string
}

const generateInlineScript = (script: Function | string) => {
if (typeof script === 'function') {
return `${script.toString()}()`
}
return String(script)
}

/**
* Embeds an AMP Script by either linking to a TS `src` file or embedding inline
* AMP Script via the `script` property. The inline script hash will automatically
Expand Down Expand Up @@ -42,10 +49,3 @@ const AmpScript: React.FC<AmpScriptProps> = ({
}

export default AmpScript

const generateInlineScript = (script: Function | string) => {
if (typeof script === 'function') {
return `${script.toString()}()`
}
return String(script)
}
8 changes: 4 additions & 4 deletions examples/cms-agilitycms/lib/components/rich-text-area.tsx
@@ -1,3 +1,7 @@
const setHTML = (textblob) => {
return { __html: textblob }
}

export default function RichTextArea({ fields }) {
const { textblob } = fields
return (
Expand All @@ -7,7 +11,3 @@ export default function RichTextArea({ fields }) {
/>
)
}

const setHTML = (textblob) => {
return { __html: textblob }
}
34 changes: 17 additions & 17 deletions examples/cms-agilitycms/lib/dependancies.ts
Expand Up @@ -3,6 +3,23 @@ const path = require('path')
const userComponentsPath = path.resolve('./components')
const libComponentsPath = path.resolve('./lib/components')

const requireComponent = (name) => {
let Component = null

try {
//check the user path first (must be relative paths)
Component = require(`../components/${name}.tsx`).default
} catch {}

if (!Component)
try {
//fallback to lib path (must be relative paths)
Component = require(`./components/${name}.tsx`).default
} catch {}

return Component
}

//Bug: when dynamic imports are used within the module, it doest not get outputted server-side
//let AgilityModule = dynamic(() => import ('../components/' + m.moduleName));

Expand Down Expand Up @@ -32,20 +49,3 @@ export const requireComponentDependancyByName = (name) => {

return Component
}

const requireComponent = (name) => {
let Component = null

try {
//check the user path first (must be relative paths)
Component = require(`../components/${name}.tsx`).default
} catch {}

if (!Component)
try {
//fallback to lib path (must be relative paths)
Component = require(`./components/${name}.tsx`).default
} catch {}

return Component
}
104 changes: 52 additions & 52 deletions examples/cms-agilitycms/lib/preview.ts
Expand Up @@ -2,45 +2,27 @@ import crypto from 'crypto'
import { getClient } from './api'
import { CMS_LANG, CMS_CHANNEL } from './constants'

//Validates whether the incoming preview request is valid
export async function validatePreview({ agilityPreviewKey, slug, contentID }) {
//Validate the preview key
if (!agilityPreviewKey) {
return {
error: true,
message: `Missing agilitypreviewkey.`,
}
}

//sanitize incoming key (replace spaces with '+')
if (agilityPreviewKey.indexOf(` `) > -1) {
agilityPreviewKey = agilityPreviewKey.split(` `).join(`+`)
}

//compare the preview key being used
const correctPreviewKey = generatePreviewKey()
//Generates a preview key to compare against
export function generatePreviewKey() {
//the string we want to encode
const str = `-1_${process.env.AGILITY_CMS_SECURITY_KEY}_Preview`

if (agilityPreviewKey !== correctPreviewKey) {
return {
error: true,
message: `Invalid agilitypreviewkey.`,
//message: `Invalid agilitypreviewkey. Incoming key is=${agilityPreviewKey} compared to=${correctPreviewKey}...`
}
//build our byte array
let data = []
for (var i = 0; i < str.length; ++i) {
data.push(str.charCodeAt(i))
data.push(0)
}

const validateSlugResponse = await validateSlugForPreview({ slug, contentID })

if (validateSlugResponse.error) {
//kickout
return validateSlugResponse
}
//convert byte array to buffer
const strBuffer = Buffer.from(data)
//encode it!
const previewKey = crypto
.createHash('sha512')
.update(strBuffer)
.digest('base64')

//return success
return {
error: false,
message: null,
slug: validateSlugResponse.slug,
}
return previewKey
}

//Checks that the requested page exists, if not return a 401
Expand Down Expand Up @@ -95,25 +77,43 @@ export async function validateSlugForPreview({ slug, contentID }) {
}
}

//Generates a preview key to compare against
export function generatePreviewKey() {
//the string we want to encode
const str = `-1_${process.env.AGILITY_CMS_SECURITY_KEY}_Preview`
//Validates whether the incoming preview request is valid
export async function validatePreview({ agilityPreviewKey, slug, contentID }) {
//Validate the preview key
if (!agilityPreviewKey) {
return {
error: true,
message: `Missing agilitypreviewkey.`,
}
}

//build our byte array
let data = []
for (var i = 0; i < str.length; ++i) {
data.push(str.charCodeAt(i))
data.push(0)
//sanitize incoming key (replace spaces with '+')
if (agilityPreviewKey.indexOf(` `) > -1) {
agilityPreviewKey = agilityPreviewKey.split(` `).join(`+`)
}

//convert byte array to buffer
const strBuffer = Buffer.from(data)
//encode it!
const previewKey = crypto
.createHash('sha512')
.update(strBuffer)
.digest('base64')
//compare the preview key being used
const correctPreviewKey = generatePreviewKey()

return previewKey
if (agilityPreviewKey !== correctPreviewKey) {
return {
error: true,
message: `Invalid agilitypreviewkey.`,
//message: `Invalid agilitypreviewkey. Incoming key is=${agilityPreviewKey} compared to=${correctPreviewKey}...`
}
}

const validateSlugResponse = await validateSlugForPreview({ slug, contentID })

if (validateSlugResponse.error) {
//kickout
return validateSlugResponse
}

//return success
return {
error: false,
message: null,
slug: validateSlugResponse.slug,
}
}
52 changes: 26 additions & 26 deletions examples/cms-builder-io/lib/api.js
Expand Up @@ -11,13 +11,6 @@ export function getAllPostsWithSlug() {
})
}

export function getAllPostsForHome(preview) {
return searchPosts(
{ 'data.slug': { $exists: true }, 'data.author': { $exists: true } },
preview
)
}

export function getDraftPost(id) {
return fetch(
`https://builder.io/api/v2/content/${BUILDER_CONFIG.postsModel}/${id}?apiKey=${BUILDER_CONFIG.apiKey}&preview=true&noCache=true&cachebust=tru&includeRefs=true`
Expand All @@ -26,25 +19,6 @@ export function getDraftPost(id) {
.then((res) => res || null)
}

export async function getPost(mongoQuery, preview) {
let post = preview
? (await searchPosts(mongoQuery, true))?.[0]
: await builder
.get(BUILDER_CONFIG.postsModel, {
includeRefs: true,
staleCacheSeconds: 20,
apiKey: BUILDER_CONFIG.apiKey,
preview: BUILDER_CONFIG.postsModel,
options: {
noTargeting: true,
},
query: mongoQuery,
})
.toPromise()

return post || null
}

export async function searchPosts(query, preview, limit = 20, offset = 0) {
let posts = await builder.getAll(BUILDER_CONFIG.postsModel, {
limit,
Expand All @@ -67,6 +41,32 @@ export async function searchPosts(query, preview, limit = 20, offset = 0) {
return posts
}

export function getAllPostsForHome(preview) {
return searchPosts(
{ 'data.slug': { $exists: true }, 'data.author': { $exists: true } },
preview
)
}

export async function getPost(mongoQuery, preview) {
let post = preview
? (await searchPosts(mongoQuery, true))?.[0]
: await builder
.get(BUILDER_CONFIG.postsModel, {
includeRefs: true,
staleCacheSeconds: 20,
apiKey: BUILDER_CONFIG.apiKey,
preview: BUILDER_CONFIG.postsModel,
options: {
noTargeting: true,
},
query: mongoQuery,
})
.toPromise()

return post || null
}

export async function getPostAndMorePosts(slug, preview, previewData) {
const post =
preview && previewData
Expand Down
36 changes: 18 additions & 18 deletions examples/cms-buttercms/lib/api.js
Expand Up @@ -16,6 +16,24 @@ try {
const defaultPageSize = 100
const defaultPostCount = 10

async function getLandingPagesData(page, pageSize = defaultPageSize) {
try {
const params = {
page,
page_size: pageSize,
}
const response = await butter.page.list('landing-page', params)

return {
pages: response?.data?.data,
prevPage: response?.data?.meta.previous_page,
nextPage: response?.data?.meta.next_page,
}
} catch (e) {
throw e.response.data.detail
}
}

export async function getLandingPage(slug) {
try {
const page = await butter.page.retrieve('landing-page', slug)
Expand All @@ -38,24 +56,6 @@ export async function getLandingPages() {
return paginatedLandingPages
}

async function getLandingPagesData(page, pageSize = defaultPageSize) {
try {
const params = {
page,
page_size: pageSize,
}
const response = await butter.page.list('landing-page', params)

return {
pages: response?.data?.data,
prevPage: response?.data?.meta.previous_page,
nextPage: response?.data?.meta.next_page,
}
} catch (e) {
throw e.response.data.detail
}
}

export async function getPostsData(
{ page, pageSize, tag, category } = { page: 1, pageSize: defaultPostCount }
) {
Expand Down

0 comments on commit acea39b

Please sign in to comment.