summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 3008e35e1154a74de6a73a8c2933e1cdd8f3f4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { cacheAllPrices, ProductRequest } from "./cron";
/**
 * Welcome to Cloudflare Workers!
 *
 * This is a template for a Scheduled Worker: a Worker that can run on a
 * configurable interval:
 * https://developers.cloudflare.com/workers/platform/triggers/cron-triggers/
 *
 * - Run `npm run dev` in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run `npm run deploy` to publish your worker
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

export interface Env {
	// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
	// MY_KV_NAMESPACE: KVNamespace;
	//
	// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
	// MY_DURABLE_OBJECT: DurableObjectNamespace;
	//
	// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
	// MY_BUCKET: R2Bucket;
	//
	// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
	// MY_SERVICE: Fetcher;
	//
	// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
	// MY_QUEUE: Queue;
	//
	// Example binding to a D1 Database. Learn more at https://developers.cloudflare.com/workers/platform/bindings/#d1-database-bindings
	DB: D1Database
}

export default {

	async fetch(request: Request, env: Env, ctx: ExecutionContext) {
		const { pathname } = new URL(request.url);
		switch (pathname) {
			case "/api/history":
				const { results } = await env.DB.prepare(
					"SELECT * FROM Prices"
				)
					.all();
				  return Response.json(results);
			case "/":
				return new Response('🀄');
			default:
				return new Response("404 💩", { status: 404 })
		}
	},

	// The scheduled handler is invoked at the interval set in our wrangler.toml's
	// [[triggers]] configuration.
	async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
		// A Cron Trigger can make requests to other endpoints on the Internet,
		// publish to a Queue, query a D1 Database, and much more.
		//
		// We'll keep it simple and make an API call to a Cloudflare API:
		// let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
		// let wasSuccessful = resp.ok ? 'success' : 'fail';

		

		const products : ProductRequest[] = [
			{productId: "888140", companyName: "Woolies"},
			// {productId: "8150288", companyName: "Coles"},
			{productId: "581170", companyName: "Woolies"},
			// {productId: "4901345", companyName: "Coles"},
			// {productId: "165367", companyName: "Woolies"},
			// {productId: "5178633", companyName: "Coles"},
			// {productId: "165367", companyName: "Woolies"},
			// {productId: "5178633", companyName: "Coles"},
			// {productId: "778171", companyName: "Woolies"},
			// {productId: "2271188", companyName: "Coles"},
			// {productId: "829281", companyName: "Woolies"},
			// {productId: "2236788", companyName: "Coles"},
		]
		const success = await cacheAllPrices(env.DB, products)
		console.log(`Fetching and caching ${products.length} products. Success: ${success}`)

		// // You could store this result in KV, write to a D1 Database, or publish to a Queue.
		// // In this template, we'll just log the result:
		// console.log(`trigger fired at ${event.cron}: ${ok}`);
	},
};