Obter uma página pelo ID
Busca uma única página pelo ID numérico.
O que este exemplo faz:
- Conecta-se à API do OneEntry
- Recupera uma página pelo seu ID numérico
- Acessa o conteúdo da página por meio de localizeInfos e attributeValues
Pré-requisitos:
- Um PROJECT_URL e um APP_TOKEN válidos para a API do OneEntry.
- Uma página configurada no OneEntry com um ID numérico conhecido (ex.: 9)
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. ComisShell: false, ele lança a exceção, e você trata os erros com try/catch. Veja Erros.
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 a página pelo ID com Pages.getPageById()
const page = await Pages.getPageById(9, 'en_US');
if ('statusCode' in page) {
throw new Error(page.message);
}
console.log(page.localizeInfos.title, page.attributeValues);
Resultado:
{
"id": 9,
"parentId": null,
"pageUrl": "home_web",
"depth": 0,
"localizeInfos": {
"title": "Home web",
"menuTitle": "Home web",
"htmlContent": "",
"plainContent": ""
},
"isVisible": true,
"blocks": [],
"type": "common_page",
"templateIdentifier": null,
"attributeSetIdentifier": null,
"attributeValues": {},
"moduleFormConfigs": [],
"isSync": false
}
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 page by ID with Pages.getPageById()
const page = await Pages.getPageById(9, 'en_US');
if ('statusCode' in page) {
throw new Error(page.message);
}
console.log(page.localizeInfos.title, page.attributeValues);