Autocodeでセッションを実装してカウンターを作ってみました。
// authenticates you with the API standard library // type `await lib.` to display API autocomplete const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN}); const cookie = require('cookie'); const { randomUUID } = require('crypto'); const appName = "session_sample"; function gen_sess_id(){ return randomUUID().split('-').join(""); } let sess_id; if (context.http.headers.hasOwnProperty('cookie')){ let cookies = cookie.parse(context.http.headers.cookie); sess_id = cookies.ACSESSID; } else { sess_id = gen_sess_id(); } let counter = 0; let sess_data = await lib.utils.kv['@0.1.16'].get({ key: `${appName}_${sess_id}` }); if(sess_data){ counter = sess_data.counter; } counter++; await lib.utils.kv['@0.1.16'].set({ key: `${appName}_${sess_id}`, value: { counter: counter }, ttl: 60 }); const body = { "sess_id": sess_id, "counter": counter }; return { headers: { 'Content-Type': 'application/json', 'Set-Cookie': cookie.serialize('ACSESSID', sess_id) }, statusCode: 200, body: Buffer.from(JSON.stringify(body)) };
Autocodeのkey-value storeのドキュメントによると、
Because storage is tied to your user account rather than to a specific token or app, you should be wary of conflicts around two different apps relying on the same key for storage. For more examples, check out the API documentation page.
らしいので、キーは、アプリ名とセッションIDを組み合わせています。あと、
You can store a maximum of 1024 key-value pairs at a time. Keys may be up to 1kb in size, and values may be up to 18kb.
あたりもあるので、AirtableなりPostgreSQLなりを使うほうが良さそうな気がします。