Skip to content

Commit

Permalink
feat: Load content from caisy using x-caisy-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed Sep 30, 2024
1 parent 5f433d5 commit 0911e12
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dist/caisy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 23 additions & 16 deletions dist/caisy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const l = (e, t) => {
},
data: [c(t)]
};
}, c = (e) => Array.isArray(e) && !e.length ? [] : e == null ? null : typeof e == "object" && !Object.keys(e).length ? {} : Array.isArray(e) ? e.map((t) => c(t)) : Object.keys(e._meta || {})?.length ? { ...c(u(e)) } : typeof e == "object" && e.json && e.json.type === "doc" ? b(e) : Object.keys(e).reduce((t, s) => {
}, c = (e) => Array.isArray(e) && !e.length ? [] : e == null ? null : typeof e == "object" && !Object.keys(e).length ? {} : Array.isArray(e) ? e.map((t) => c(t)) : Object.keys(e._meta || {})?.length ? { ...c(u(e)) } : typeof e == "object" && e.json && e.json.type === "doc" ? w(e) : Object.keys(e).reduce((t, s) => {
const r = e[s];
return r == null ? (t[s] = null, t) : Array.isArray(r) ? (t[s] = r.map((n) => c(n)), t) : typeof r == "object" ? (t[s] = { ...c(u(r)) }, t) : (t[s] = r, t);
}, {}), u = (e) => {
Expand All @@ -54,7 +54,7 @@ const l = (e, t) => {
height: e.height,
width: e.width
}
}), b = (e) => e.connections ? !e.json || typeof e.json == "string" ? "" : {
}), w = (e) => e.connections ? !e.json || typeof e.json == "string" ? "" : {
content: e.json.content.map((s) => {
if (s.type !== "documentLink" || !e.connections)
return s;
Expand Down Expand Up @@ -90,7 +90,8 @@ const l = (e, t) => {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
"x-caisy-preview": !0
},
body: JSON.stringify({
query: s
Expand All @@ -104,7 +105,8 @@ const l = (e, t) => {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
"x-caisy-preview": !0
},
body: JSON.stringify({
query: s,
Expand All @@ -117,17 +119,18 @@ const l = (e, t) => {
} catch (a) {
throw new Error(a.message);
}
}, P = async (e) => {
const { projectId: t, query: s, perPage: r, after: n = "", page: a, ...o } = e, m = d(t), i = Number.parseInt(e.page ?? "1"), f = Number.parseInt(e.perPage ?? "10"), p = n ? f : (i > 1 ? i - 1 : i) * f, h = await fetch(m, {
}, b = async (e) => {
const { projectId: t, query: s, perPage: r, after: n = "", page: a, ...o } = e, p = d(t), i = Number.parseInt(e.page ?? "1"), f = Number.parseInt(e.perPage ?? "10"), m = n ? f : (i > 1 ? i - 1 : i) * f, h = await fetch(p, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
"x-caisy-preview": !0
},
body: JSON.stringify({
query: s,
variables: {
first: p,
first: m,
after: n,
...o
}
Expand All @@ -136,27 +139,31 @@ const l = (e, t) => {
if (!y.data)
return [];
const { endCursor: j, hasNextPage: S } = y.data[Object.keys(y.data)[0]].pageInfo;
return i === 1 || !S || n ? l(y.data[Object.keys(y.data)[0]], i.toString()) : await P({
return i === 1 || !S || n ? l(
y.data[Object.keys(y.data)[0]],
i.toString()
) : await b({
projectId: t,
query: s,
perPage: r,
...o,
page: i.toString(),
after: j
});
}, E = async (e) => {
}, v = async (e) => {
const { projectId: t, query: s } = e, r = d(t), n = await fetch(r, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
"x-caisy-preview": !0
},
body: JSON.stringify({
query: s
})
}), a = await g(n), o = a.data[Object.keys(a.data)[0]];
return w(o);
}, w = (e) => {
return P(o);
}, P = (e) => {
const t = {
pageInfo: {},
edges: [
Expand All @@ -169,11 +176,11 @@ const l = (e, t) => {
};
export {
A as getEntities,
P as getEntitiesWithPagination,
b as getEntitiesWithPagination,
_ as getEntityByAttribute,
E as getSingleEntityType,
v as getSingleEntityType,
O as normalizeCaisyAssetData,
C as normalizeCaisyItemContent,
l as normalizeCaisyListContent,
w as normalizeSingleTypeAsList
P as normalizeSingleTypeAsList
};
2 changes: 1 addition & 1 deletion dist/caisy/entities.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/caisy/entitiesByPage.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/caisy/entityByAttribute.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions src/caisy/entities.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { getAPIUrlByProjectId, handleFetchResponse, normalizeList } from "./utils";
import { getAPIUrlByProjectId, handleFetchResponse, normalizeList } from './utils'

export const getEntitiesData = async (params: {
projectId: string,
query: string,
}
) => {
export const getEntitiesData = async (params: { projectId: string; query: string }) => {
const { projectId, query } = params
const url = getAPIUrlByProjectId(projectId)

const response = await fetch(url, {
method: "POST",
method: 'POST',
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
'Content-Type': 'application/json',
'x-caisy-token': process.env.CMS_ACCESS_TOKEN,
'x-caisy-preview': true,
},
body: JSON.stringify({
query,
Expand All @@ -21,4 +18,4 @@ export const getEntitiesData = async (params: {

const responseJSON = await handleFetchResponse(response)
return normalizeList(responseJSON.data[Object.keys(responseJSON.data)[0]])
}
}
32 changes: 19 additions & 13 deletions src/caisy/entitiesByPage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAPIUrlByProjectId, handleFetchResponse, normalizeList } from "./utils";
import { getAPIUrlByProjectId, handleFetchResponse, normalizeList } from './utils'

/**
* When getting entities by page in caisy we might need to do multiple calls
Expand All @@ -10,34 +10,37 @@ import { getAPIUrlByProjectId, handleFetchResponse, normalizeList } from "./util
* https://caisy.io/developer/docs/external-api/queries-pagination#top
*/
export const getEntitiesByPage = async (params: {
projectId: string,
query: string,
page: string,
perPage: string,
projectId: string
query: string
page: string
perPage: string
after?: string
}) => {
// restParams is used for sending the filter parameters for the query
const { projectId, query, perPage, after = '', page, ...restParams } = params

Check warning on line 20 in src/caisy/entitiesByPage.ts

View workflow job for this annotation

GitHub Actions / test

'page' is assigned a value but never used
const url = getAPIUrlByProjectId(projectId)

const requestedPage = Number.parseInt(params['page'] ?? "1")
const requestedPerPage = Number.parseInt(params['perPage'] ?? "10")
const requestedPage = Number.parseInt(params['page'] ?? '1')
const requestedPerPage = Number.parseInt(params['perPage'] ?? '10')

const firstParam = !after ? (requestedPage > 1 ? requestedPage -1 : requestedPage ) * requestedPerPage : requestedPerPage
const firstParam = !after
? (requestedPage > 1 ? requestedPage - 1 : requestedPage) * requestedPerPage
: requestedPerPage

const response = await fetch(url, {
method: "POST",
method: 'POST',
headers: {
"Content-Type": "application/json",
"x-caisy-token": process.env.CMS_ACCESS_TOKEN,
'Content-Type': 'application/json',
'x-caisy-token': process.env.CMS_ACCESS_TOKEN,
'x-caisy-preview': true,
},
body: JSON.stringify({
query,
variables: {
first: firstParam,
after,
...restParams,
}
},
}),
})

Expand All @@ -53,7 +56,10 @@ export const getEntitiesByPage = async (params: {
// if hasNextPage is false, no need to look for the next page, just return this one for now
// if requestedPage is 1, no need for the second call
if (requestedPage === 1 || !hasNextPage || after) {
return normalizeList(responseJSON.data[Object.keys(responseJSON.data)[0]], requestedPage.toString())
return normalizeList(
responseJSON.data[Object.keys(responseJSON.data)[0]],
requestedPage.toString()
)
}

return await getEntitiesByPage({
Expand Down
1 change: 1 addition & 0 deletions src/caisy/entityByAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const getDataByAttribute = async (params: {
headers: {
'Content-Type': 'application/json',
'x-caisy-token': process.env.CMS_ACCESS_TOKEN,
'x-caisy-preview': true,
},
body: JSON.stringify({
query,
Expand Down
1 change: 1 addition & 0 deletions src/caisy/singleEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getSingleEntity = async (params: { projectId: string; query: string
headers: {
'Content-Type': 'application/json',
'x-caisy-token': process.env.CMS_ACCESS_TOKEN,
'x-caisy-preview': true,
},
body: JSON.stringify({
query,
Expand Down

0 comments on commit 0911e12

Please sign in to comment.