Browse Source

small todo changes and refactoring API

main
deicidus 2 years ago
parent
commit
fdb34f803c
  1. 22
      package-lock.json
  2. 1
      package.json
  3. 42
      src/ao-lib/api.js
  4. 2
      src/routes/api/index.json.js
  5. 7
      src/routes/index.svelte

22
package-lock.json generated

@ -38,6 +38,7 @@
"chalk": "^5.0.1", "chalk": "^5.0.1",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"date-fns": "^2.28.0", "date-fns": "^2.28.0",
"envfile": "^6.17.0",
"eslint": "^8.12.0", "eslint": "^8.12.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^4.0.0", "eslint-plugin-svelte3": "^4.0.0",
@ -1904,6 +1905,21 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/envfile": {
"version": "6.17.0",
"resolved": "https://registry.npmjs.org/envfile/-/envfile-6.17.0.tgz",
"integrity": "sha512-RnhtVw3auDZeeh5VtaNrbE7s6Kq8BoRtGIzcbMpMsJ+wIpRgs5jiDG4gQjW+vfws5QPlizE57/fUU0Tj6Nrs8A==",
"dev": true,
"bin": {
"envfile": "bin.cjs"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://bevry.me/fund"
}
},
"node_modules/es6-promise": { "node_modules/es6-promise": {
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz",
@ -7674,6 +7690,12 @@
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz",
"integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg==" "integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg=="
}, },
"envfile": {
"version": "6.17.0",
"resolved": "https://registry.npmjs.org/envfile/-/envfile-6.17.0.tgz",
"integrity": "sha512-RnhtVw3auDZeeh5VtaNrbE7s6Kq8BoRtGIzcbMpMsJ+wIpRgs5jiDG4gQjW+vfws5QPlizE57/fUU0Tj6Nrs8A==",
"dev": true
},
"es6-promise": { "es6-promise": {
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz",

1
package.json

@ -27,6 +27,7 @@
"chalk": "^5.0.1", "chalk": "^5.0.1",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"date-fns": "^2.28.0", "date-fns": "^2.28.0",
"envfile": "^6.17.0",
"eslint": "^8.12.0", "eslint": "^8.12.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^4.0.0", "eslint-plugin-svelte3": "^4.0.0",

42
src/ao-lib/api.js

@ -3,11 +3,11 @@ import { v1 as uuidV1 } from 'uuid'
import { io } from 'socket.io-client' import { io } from 'socket.io-client'
import { createHash, hmacHex } from './crypto.js' import { createHash, hmacHex } from './crypto.js'
import { isObject } from './util.js' import { isObject } from './util.js'
import { aoEnv } from './settings.js' //import { aoEnv } from './settings.js'
// The AO API server endpoint this ao-cli client will attempt to connect to // The AO API server endpoint this ao-cli client will attempt to connect to
export const AO_DEFAULT_HOSTNAME = 'localhost:8003' export const AO_DEFAULT_HOSTNAME = 'localhost:8003'
const HOSTNAME = aoEnv('AO_CLI_TARGET_HOSTNAME') || AO_DEFAULT_HOSTNAME const HOSTNAME = /*aoEnv('AO_CLI_TARGET_HOSTNAME') ||*/ AO_DEFAULT_HOSTNAME
const [HOST, PORT] = HOSTNAME.split(':') const [HOST, PORT] = HOSTNAME.split(':')
// The AO API server websocket endpoint this ao-cli client will attempt to connect to // The AO API server websocket endpoint this ao-cli client will attempt to connect to
@ -20,9 +20,9 @@ export const socket = io(AO_SOCKET_URL, {
export let socketStatus export let socketStatus
// Load the current session cookies from the AO .env file // Load the current session cookies from the AO .env file
let currentMemberId = aoEnv('AO_CLI_SESSION_MEMBERID') let currentMemberId = '13844ae0-2270-11ea-b45c-83ea6e9b1aa1' //aoEnv('AO_CLI_SESSION_MEMBERID')
let currentSessionId = aoEnv('AO_CLI_SESSION_ID') let currentSessionId = 'c5c9ccd0-f28d-11ec-9614-15596c298860' //aoEnv('AO_CLI_SESSION_ID')
let currentSessionToken = aoEnv('AO_CLI_SESSION_TOKEN') let currentSessionToken = '51ad3b959b89d859786e232c72cda792ce5e99ff9dbb639d24f8ae45a70ff753' //aoEnv('AO_CLI_SESSION_TOKEN')
// Performs a GET request to the specified endpoint, sending the given payload // Performs a GET request to the specified endpoint, sending the given payload
export async function getRequest(endpoint, payload = null, alternateHost = null, verbose = true) { export async function getRequest(endpoint, payload = null, alternateHost = null, verbose = true) {
@ -49,6 +49,7 @@ export async function postRequest(endpoint, payload = null, verbose = true) {
} }
try { try {
if(payload) { if(payload) {
console.log("about to post to", HOSTNAME, currentSessionToken)
return await request return await request
.post(HOSTNAME + endpoint) .post(HOSTNAME + endpoint)
.send(payload) .send(payload)
@ -70,6 +71,25 @@ export async function postEvent(event, verbose) {
return await postRequest('/events', event, verbose) return await postRequest('/events', event, verbose)
} }
// Attempts login with a new guest session. If successful, returns the generated session and token (login cookies).
// Guest sessions be be convertable to permanent sessions by setting a password on them
export async function createGuestSession() {
const session = uuidV1()
//const user = 'anon'
let sessionKey = createHash(session)
const token = hmacHex(session, sessionKey)
const result = await request
.post(HOSTNAME + '/session')
.set('authorization', token)
.set('session', session)
//.set('name', user)
.on('error', () => false)
currentMemberId = result.body.memberId
currentSessionToken = token
currentSessionId = session
return { session, token, memberId: currentMemberId }
}
// Attempts login with the given username and password combo. If successful, returns the generated session and token (login cookies). // Attempts login with the given username and password combo. If successful, returns the generated session and token (login cookies).
export async function createSession(user, pass) { export async function createSession(user, pass) {
const session = uuidV1() const session = uuidV1()
@ -281,18 +301,22 @@ export async function cacheMeme(taskId) {
// Cards feature // Cards feature
// Returns the card and other cards as specified by the alsoGetRelevant arg // Returns the card and other cards as specified by the alsoGetRelevant arg
// If multiple cards are returned, they will be returned in their global deck order (global creation order on server) // If multiple cards are returned, they will be returned in their global deck order (global creation order on server)
export async function getCard(taskId, alsoGetRelevant = 'subcards') { export async function getCard(taskId, alsoGetRelevant = 'subcards', verbose = true) {
console.log('about to getCard, taskId: ', taskId)
taskId = taskId.trim().toLowerCase() taskId = taskId.trim().toLowerCase()
let payload = { taskId: taskId } let payload = { taskIds: [ taskId ] }
const result = await postRequest('/fetchTaskByID', payload, false) // todo: change to flat text, not JSON (?) console.log('payload', payload)
const result = await postRequest('/fetchTasks', payload, verbose) // todo: change to flat text, not JSON (?)
console.log('result', result)
if(!result || !result.body) { if(!result || !result.body) {
//console.log('Error fetching task.') console.log('Error fetching task.')
return null return null
} }
if(alsoGetRelevant) { if(alsoGetRelevant) {
let relevantCards = await getAllRelevantCards(result.body, alsoGetRelevant) let relevantCards = await getAllRelevantCards(result.body, alsoGetRelevant)
return [result.body, ...relevantCards] return [result.body, ...relevantCards]
} }
console.log('result', result.body)
return [result.body] return [result.body]
} }

2
src/routes/api/index.json.js

@ -1,6 +1,6 @@
export async function get() { export async function get() {
return { return {
status: 200, status: 200,
body: 'Hello World! ' + process.env['NODE_AO_DB_FILE'], body: 'Hello, World! ' + process.env['NODE_AO_DB_FILE'],
} }
} }

7
src/routes/index.svelte

@ -10,12 +10,15 @@
} }
} }
} }
import { createGuestSession } from '../ao-lib/api.js'
const sessionId = createGuestSession()
</script> </script>
<script lang="ts"> <script lang="ts">
import LoginBox from '/src/components/LoginBox.svelte' import LoginBox from '/src/components/LoginBox.svelte'
import Card from '../components/Card.svelte' import Card from '../components/Card.svelte'
import Priorities from '../components/Priorities.svelte' //import Priorities from '../components/Priorities.svelte'
export let apiTestResult export let apiTestResult
console.log('Hello World!', apiTestResult) console.log('Hello World!', apiTestResult)
@ -106,7 +109,7 @@
</svelte:head> </svelte:head>
<h1>AO SvelteKit</h1> <h1>AO SvelteKit</h1>
<Priorities /> <!--<Priorities />-->
<LoginBox /> <LoginBox />
<h2>themes test</h2> <h2>themes test</h2>
<audio id="sound1" preload="auto" src={themeSounds[currentTheme]} /> <audio id="sound1" preload="auto" src={themeSounds[currentTheme]} />

Loading…
Cancel
Save