#fetch

1 messages · Page 1 of 1 (latest)

candid pumice
#

Give me an example fetch for an API

loud impBOT
#

Hi @candid pumice. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, here's an example of how you can use fetch to get data from an API in Next.js:

async function getData() {
 const res = await fetch('<https://api.example.com/...'>)
 // The return value is *not* serialized
 // You can return Date, Map, Set, etc.
 
 if (!res.ok) {
 // This will activate the closest `error.js` Error Boundary
 throw new Error('Failed to fetch data')
 }
 
 return res.json()
}
 
export default async function Page() {
 const data = await getData()
 
 return <main></main>
}

In this example, fetch is used to make a request to <https://api.example.com/....> If the request is successful, the response is converted to JSON and returned. If the request fails, an error is thrown.

You can use fetch with async/await in Server Components, in Route Handlers, and in Server Actions.

Please note that Next.js extends the native fetch Web API to allow you to configure the caching and revalidating behavior for each fetch request on the server. React extends fetch to automatically memoize fetch requests while rendering a React component tree.

Source (https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-fetch)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@candid pumice kapa.ai is still learning and improving, please let us know how it did by reacting below

candid pumice
#

@loud imp
with drizzle and turso, inside my scheme.ts i have to do something like this

import { sql } from 'drizzle-orm';
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';

export const users = sqliteTable('users', {
id: integer('id').primaryKey(),
name: text('name').notNull(),
email: text('email').unique().notNull(),
});

export const posts = sqliteTable('posts', {
id: integer('id').primaryKey(),
title: text('title').notNull(),
content: text('content').notNull(),
createdAt: text('created_at')
.default(sqlCURRENT_TIMESTAMP)
.notNull(),
});

export type InsertUser = typeof users.$inferInsert;
export type SelectUser = typeof users.$inferSelect;

export type InsertPost = typeof posts.$inferInsert;
export type SelectPost = typeof posts.$inferSelect;

but i just need to read products with the following structure:

// {
// id: 0,
// img: "/prod.jpg",
// name: "prod1",
// price: "150",
// descripcion: "lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj",
// category: ["comp", "all"],
// sale: true,
// stock: true,
// },
// {
// id: 1,
// img: "/prod2.jpg",
// name: "prod2",
// price: "150",
// descripcion: "lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj lorem impsum iaiaasasdasndasjidasdjaskdsjjdkasjdjskjsasdj",
// category: ["comp", "all"],
// sale: true,
// stock: true,
// },

loud impBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the information provided in the knowledge sources, you can define a products table using Drizzle ORM with the structure you provided. Here's an example:

import { drizzle } from 'drizzle-orm/vercel-postgres';
import { sql } from '@vercel/postgres';
import {
 pgTable,
 serial,
 text,
 boolean,
} from 'drizzle-orm/pg-core';

export const db = drizzle(sql);

export const ProductsTable = pgTable(
 'products',
 {
 id: serial('id').primaryKey(),
 img: text('img').notNull(),
 name: text('name').notNull(),
 price: text('price').notNull(),
 description: text('description').notNull(),
 category: text('category').notNull(),
 sale: boolean('sale').notNull(),
 stock: boolean('stock').notNull(),
 },
);

export const getProductsTable = async () => {
 const selectResult = await db.select().from(ProductsTable);
 console.log('Results', selectResult);
};

In this example, we're defining a products table with fields id, img, name, price, description, category, sale, and stock. The getProductsTable function is used to fetch all products from the products table.

Please note that this is a basic example and you might need to adjust it based on your actual database schema and requirements.

Source (https://vercel.com/docs/storage/vercel-postgres/using-an-orm)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@candid pumice kapa.ai is still learning and improving, please let us know how it did by reacting below

candid pumice
#

@loud imp
we are doing it with turso btw

loud impBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry, but the knowledge sources provided do not contain information on how to use Turso with Drizzle ORM or any other specific instructions related to Turso. Therefore, I'm unable to provide a confident answer to your question. I recommend checking the official documentation or reaching out to the Turso community for more specific guidance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@candid pumice kapa.ai is still learning and improving, please let us know how it did by reacting below

candid pumice
#

i did this, but it says
export const posts = sqliteTable('posts', {
id: integer('id').primaryKey(),
img: text('img').notNull(),
name: text('name').notNull(),
price: text('price').notNull(),
description: text('description').notNull(),
category: text('category').notNull(),
sale: boolean('sale').notNull(),
stock: boolean('stock').notNull(),
});

'boolean' only refers to a type, but is being used as a value here.

#

@loud imp
i did this, but it says
export const posts = sqliteTable('posts', {
id: integer('id').primaryKey(),
img: text('img').notNull(),
name: text('name').notNull(),
price: text('price').notNull(),
description: text('description').notNull(),
category: text('category').notNull(),
sale: boolean('sale').notNull(),
stock: boolean('stock').notNull(),
});

'boolean' only refers to a type, but is being used as a value here.

loud impBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry, but the knowledge sources provided do not contain information on how to define a boolean field with Drizzle ORM or any other specific instructions related to your issue. Therefore, I'm unable to provide a confident answer to your question. I recommend checking the official documentation or reaching out to the Drizzle ORM community for more specific guidance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: