Pular para o conteúdo principal

Obter todas as páginas

Busca todas as páginas do projeto, e não apenas as raiz.

O que este exemplo faz:

  • Conecta-se à API do OneEntry
  • Recupera todas as páginas de todos os níveis de profundidade em uma única chamada
  • Filtra as páginas por tipo usando o campo type

Pré-requisitos:

  • Um PROJECT_URL e um APP_TOKEN válidos para a API do OneEntry.
  • Pelo menos uma página configurada no OneEntry.

Observações:

  • Por padrão (isShell: true), o SDK retorna um objeto de erro em vez de lançar uma exceção, então verifique o resultado, por exemplo 'statusCode' in result. Com isShell: false, ele lança a exceção, e você trata os erros com try/catch. Veja Erros.
  • Retorna um array simples, e não { items, total }. Ele inclui páginas de todas as profundidades; use o campo depth ou parentId para montar uma árvore.
  • O campo type é "common_page" para páginas de conteúdo, "catalog_page" para páginas com produtos, "error_page" para páginas de erro ou "external_page" para links para URLs externas.
  • O campo products em cada objeto de página mostra a quantidade de produtos atribuídos a essa página.

Documentação da plataforma:

Referência do SDK:

Experimente ao vivo​

Execute este exemplo no sandbox do JS SDK. Conecte a URL do seu projeto e o App Token na primeira visita e, em seguida, abra:

Cenário​

1. Importar defineOneEntry e definir PROJECT_URL e APP_TOKEN​

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';

2. Criar um cliente da API com defineOneEntry()​

const { Pages } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Obter todas as páginas com Pages.getPages()​

// Returns a plain array (NOT `{ items, total }`)
const pages = await Pages.getPages('en_US');

if ('statusCode' in pages) {
throw new Error(pages.message);
}
Resultado:
[
{
"id": 9,
"config": {},
"depth": 1,
"parentId": 8,
"pageUrl": "blog1",
"attributeSetIdentifier": null,
"localizeInfos": {
"title": "Blog 1",
"htmlContent": "",
"plainContent": "",
"menuTitle": "Blog 1"
},
"isVisible": true,
"products": 0,
"childrenCount": 0,
"type": "common_page",
"position": 2,
"templateIdentifier": null,
"isSync": false,
"categoryPath": "blog/blog1",
"rating": {},
"attributeValues": {},
"moduleFormConfigs": []
},
{
"id": 8,
"config": {},
"depth": 0,
"parentId": null,
"pageUrl": "blog",
"attributeSetIdentifier": "page",
"localizeInfos": {
"title": "Blog",
"htmlContent": "",
"plainContent": "",
"menuTitle": "Blog"
},
"isVisible": true,
"products": 0,
"childrenCount": 2,
"type": "common_page",
"position": 3,
"templateIdentifier": "template",
"isSync": true,
"categoryPath": "blog",
"rating": {},
"attributeValues": {
"text": {
"type": "string",
"value": "some text",
"isIcon": false,
"position": 0,
"additionalFields": {},
"isProductPreview": false
},
"number": {
"type": "integer",
"value": 5,
"isIcon": false,
"position": 1,
"additionalFields": {},
"isProductPreview": false
},
"real": {
"type": "real",
"value": 10,
"isIcon": false,
"position": 2,
"additionalFields": {},
"isProductPreview": false
},
"float": {
"type": "float",
"value": 12,
"isIcon": false,
"position": 3,
"additionalFields": {},
"isProductPreview": false
}
},
"moduleFormConfigs": [
{
"id": 9,
"formIdentifier": "full_form",
"moduleIdentifier": "content",
"isGlobal": false,
"isClosed": false,
"isModerate": true,
"viewOnlyMyData": false,
"commentOnlyMyData": false,
"entityIdentifiers": [
{
"id": "blog",
"isNested": false
}
],
"formDataCount": 6281,
"ratingCalculation": "average",
"allowHalfRatings": null,
"showAggregatedRating": null,
"maxRatingScale": null,
"isAnonymous": null,
"allowRerating": null,
"isRating": null,
"entityFormDataCount": {
"blog": 6281
},
"nestedEntityIdentifiers": [],
"exceptionIds": []
},
{
"id": 3,
"formIdentifier": "file_form",
"moduleIdentifier": "content",
"isGlobal": false,
"isClosed": false,
"isModerate": false,
"viewOnlyMyData": false,
"commentOnlyMyData": false,
"entityIdentifiers": [
{
"id": "blog",
"isNested": false
}
],
"formDataCount": 691,
"ratingCalculation": "average",
"allowHalfRatings": null,
"showAggregatedRating": null,
"maxRatingScale": null,
"isAnonymous": null,
"allowRerating": null,
"isRating": null,
"entityFormDataCount": {
"blog": 691
},
"nestedEntityIdentifiers": [],
"exceptionIds": []
},
{
"id": 2,
"formIdentifier": "test-form",
"moduleIdentifier": "content",
"isGlobal": false,
"isClosed": false,
"isModerate": false,
"viewOnlyMyData": false,
"commentOnlyMyData": false,
"entityIdentifiers": [
{
"id": "blog",
"isNested": false
}
],
"formDataCount": 909,
"ratingCalculation": "average",
"allowHalfRatings": null,
"showAggregatedRating": null,
"maxRatingScale": null,
"isAnonymous": null,
"allowRerating": null,
"isRating": null,
"entityFormDataCount": {
"blog": 909
},
"nestedEntityIdentifiers": [],
"exceptionIds": []
}
],
"template": {
"id": 4,
"attributeSetIdentifier": null,
"title": "Template",
"generalTypeId": 4,
"identifier": "template",
"version": 0,
"generalTypeName": "catalog_page",
"attributeValues": {},
"position": 1
}
},
{
"id": 49,
"config": {},
"depth": 1,
"parentId": 8,
"pageUrl": "blog2",
"attributeSetIdentifier": null,
"localizeInfos": {
"title": "Blog 2",
"htmlContent": "",
"plainContent": "",
"menuTitle": "Blog 2"
},
"isVisible": true,
"products": 0,
"childrenCount": 0,
"type": "common_page",
"position": 1,
"templateIdentifier": null,
"isSync": false,
"categoryPath": "blog2",
"rating": {},
"attributeValues": {},
"moduleFormConfigs": []
}
]

4. Filtrar e acessar os campos da página​

const catalogPages = pages.filter((p: { type: string }) => p.type === 'catalog_page');
console.log('catalogPages ', catalogPages);
const commonPages = pages.filter((p: { type: string }) => p.type === 'common_page');
console.log('commonPages ', commonPages);

pages.forEach((page) => {
const title = page.localizeInfos?.title;
const type = page.type; // "common_page" | "catalog_page" | "error_page" | "external_page"
const pageUrl = page.pageUrl;
const depth = page.depth; // 0 for root, 1+ for children
const parentId = page.parentId; // null for root pages
console.log(title, type, pageUrl, depth, parentId);
});
Resultado (valores de uma página):
{
"title": "Services",
"type": "catalog_page",
"pageUrl": "services",
"depth": 0,
"parentId": null
}

Exemplo final​

// 1. Import defineOneEntry and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';

// 2. Create an API client with defineOneEntry()
const { Pages } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Get all pages with Pages.getPages()
// Returns a plain array (NOT `{ items, total }`)
const pages = await Pages.getPages('en_US');

if ('statusCode' in pages) {
throw new Error(pages.message);
}

// 4. Filter and access page fields
const catalogPages = pages.filter((p: { type: string }) => p.type === 'catalog_page');
console.log('catalogPages ', catalogPages);
const commonPages = pages.filter((p: { type: string }) => p.type === 'common_page');
console.log('commonPages ', commonPages);

pages.forEach((page) => {
const title = page.localizeInfos?.title;
const type = page.type; // "common_page" | "catalog_page" | "error_page" | "external_page"
const pageUrl = page.pageUrl;
const depth = page.depth; // 0 for root, 1+ for children
const parentId = page.parentId; // null for root pages
console.log(title, type, pageUrl, depth, parentId);
});