Open rhcndrsn opened 7 months ago
Good question. @karthik2804 Do you know if the query object on the itty-router Request needs to be plumbed through the SDK? Or should it work already?
Hey @rhcndrsn,
you can find a full-fledged CRUD implementation using different capabilities from the router at https://github.com/fermyon/enterprise-architectures-and-patterns/tree/main/http-crud-js-sqlite.
On top of those could you elaborate a bit about the query string requirements? It's not 100% clear to me what you wanna achieve:
Hi,
actually i need the query parameters for filtering the data. I mean, if i asking localhost:3000/users page and i want to filter them with params;
localhost:3000/users?age=30&location=Turkey
like that. I hope i can tell what i need.
thank you,
Thanks for coming back to this one @rhcndrsn
You can access the query string from within your Spin App. When adding your handlers to the Router.
With the recently released new JavaScript SDK for Spin (see https://www.fermyon.com/blog/introducing-the-new-js-sdk)
This could look like this
import { Router } from "@fermyon/spin-sdk";
const router = Router();
router.get("/users", ({ query }, _, res) => getUsers(query, res));
export async function handler(req, res) {
return await router.handleRequest(req, res);
}
function getUsers(query, res) {
console.log(`Received query string: ${JSON.stringify(query)}`);
res.set({ "content-type": "application/json" })
res.send(JSON.stringify({
baz: query["foo"]
}));
}
Although the part of pulling values from the query string is hardcoded, you can see how the value of foo
(from the query string) is pulled and sent back as the value of baz
. This would result in the following
curl -iX GET http://localhost:3000/users\?foo\=bar
HTTP/1.1 200 OK
content-type: application/json
content-length: 13
date: Mon, 12 Aug 2024 09:05:43 GMT
{"baz":"bar"}%
Hi,
Im trying to test fermyon/spin-sdk with ts but i cannot find any document about routing and query string samples.
I just see that sample in js routing page,
localhost:3000/home/param
but i need this localhost:3000/home?param1=value1¶m2=value2.....¶mN=valueN
i need query string routing and handle the params.. i saw itty-route link on there too.. but its not working with spin-sdk
have any sample or solutions for that?