Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utilize more types in LFNext #1664

Merged
merged 61 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
e437c5d
corrected file extension
Dec 8, 2022
851687f
typed sf call
Dec 9, 2022
b250349
missed a possibility
Dec 9, 2022
be97967
typed the activities api
Dec 9, 2022
e6aed83
cleaned up some param syntax
Dec 9, 2022
b7b40ec
Merge branch 'develop' into chore/types
Dec 13, 2022
1a48cba
Merge branch 'develop' into chore/types
Dec 21, 2022
b06a2f9
changed extensions for TS
Dec 21, 2022
a65686c
upgraded more deps
Dec 21, 2022
eb03cef
resolved ts warning
Dec 21, 2022
0610f49
preferring type over interface
Dec 21, 2022
658dc7a
type the event
Dec 21, 2022
db04118
class attr not needed at this point
Dec 21, 2022
872c9db
class attr not needed
Dec 21, 2022
c0f60f1
cleaned up sf wrapper
Dec 21, 2022
8af30c1
tried different typing approach
Dec 21, 2022
14a5e22
typed dashboard page
Dec 21, 2022
1025ae0
renamed result
Dec 21, 2022
7d85c9a
typed some of the dashboard code
Dec 22, 2022
a029d7b
fixed some "undefined" classes showing up in final HTML
Dec 22, 2022
ac5cc79
gave up on correcting this ts error
Dec 22, 2022
5e96bb4
typed the Input comp
Dec 22, 2022
411f350
didn't want playground to autoscroll to the focused input
Dec 22, 2022
ab8a30e
typed sf calls
Dec 22, 2022
22b01b7
typed Progress comp
Dec 22, 2022
67f89a5
typed client-side fetch normalizer
Dec 23, 2022
1d35ae7
removed tsc warning
Dec 23, 2022
0c5a75f
addressed tsc warning
Dec 23, 2022
0d2151a
type the Sf response
Dec 23, 2022
26a3e2d
resolved some tsc warnings
Dec 23, 2022
45a6f87
addressed some type hints
Dec 23, 2022
4e208e2
typed user call
Dec 23, 2022
9f4a8fc
typed project details call a little more
Dec 23, 2022
7bfcb3b
addressed a couple more type warnings
Dec 23, 2022
1a8f9fe
consistent convention
Dec 24, 2022
9741324
typed some transforms
Dec 24, 2022
6b0b3d8
consistent convention
Dec 24, 2022
815c0c6
Merge branch 'develop' into chore/types
Dec 24, 2022
b821149
Merge branch 'develop' into chore/types
Jan 3, 2023
d6448da
Merge branch 'develop' into chore/types
Jan 4, 2023
02061b4
incorporated PR feedback
Jan 4, 2023
c5323cc
incorporated PR feedback
Jan 4, 2023
31d2e35
incorporated PR feedback
Jan 4, 2023
684a7f1
incorporated PR feedback
Jan 4, 2023
4ee2094
incorporated PR feedback
Jan 4, 2023
9f1fce8
Merge branch 'develop' into chore/types
Jan 5, 2023
fccc305
simplified audio/picture detection algorithm
Jan 5, 2023
cc966de
incorporated PR feedback
Jan 5, 2023
20f57f6
eliminated use of types.d.ts files
Jan 6, 2023
c2d0749
Merge branch 'develop' into chore/types
Jan 6, 2023
5123da7
Merge branch 'develop' into chore/types
Jan 6, 2023
00f3bf6
updated deps
Jan 6, 2023
d7ecf3b
incorporated PR feedback
Jan 9, 2023
d6dac14
incorporated PR feedback
Jan 9, 2023
31338f2
eliminated remaining `svelte check` warnings
Jan 9, 2023
3046119
accepted PR suggestion
Jan 9, 2023
3e02ce2
incorporated PR feedback
Jan 9, 2023
85cef15
accepted PR suggestion
Jan 9, 2023
c3b7322
incorporated PR feedback
Jan 9, 2023
e45a2d2
simplified error handling
Jan 9, 2023
776d44c
Merge branch 'develop' into chore/types
Jan 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions next-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"clean": "rimraf .svelte-kit build node_modules && echo '🔔 reminder: you will need to reinstall deps, `npm i`'"
},
"devDependencies": {
"@sveltejs/adapter-node": "next",
"@sveltejs/kit": "next",
"@sveltejs/adapter-node": "^1",
"@sveltejs/kit": "^1",
"@tailwindcss/typography": "^0.5.2",
"autoprefixer": "^10",
"daisyui": "^2",
"rimraf": "^3",
"svelte": "^3",
"svelte-check": "^2",
"tailwindcss": "^3.0.23",
"tailwindcss": "^3",
"tslib": "^2",
"typescript": "^4",
"vite": "^4"
Expand Down
4 changes: 2 additions & 2 deletions next-app/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
<body data-sveltekit-preload-data=hover>
<div style='display: contents'>%sveltekit.body%</div>
</body>
</html>
2 changes: 1 addition & 1 deletion next-app/src/lib/PageHeader.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1 class='text-2xl md:text-3xl { $$props.class }'>
<h1 class='text-2xl md:text-3xl'>
<slot />
</h1>
4 changes: 2 additions & 2 deletions next-app/src/lib/error/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { browser } from '$app/environment'
import { writable, type Writable } from 'svelte/store'

interface LfError {
type LfError = {
message: string,
code?: number,
}
Expand All @@ -11,7 +11,7 @@ export function throw_error(message: string, code: number = 0) {
throw set({ message, code })
}

export const dismiss = set
export const dismiss = () => set({ message: '' })

if (browser) {
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror#window.addEventListenererror
Expand Down
49 changes: 19 additions & 30 deletions next-app/src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import { throw_error } from '$lib/error'
import { start, stop } from '$lib/progress'
import type { AdaptedFetchArgs, FetchArgs } from './types'

export async function CREATE(url, body) { return await custom_fetch('post' , url, body) }
export async function GET (url ) { return await custom_fetch('get' , url ) }
export async function UPDATE(url, body) { return await custom_fetch('put' , url, body) }
export async function DELETE(url ) { return await custom_fetch('delete', url ) }

// https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData
// export const upload = async formData => await CREATE('post', formData)
export async function CREATE({url, body}: FetchArgs) { return await adapted_fetch({method: 'POST' , url, body}) }
export async function GET ({url }: FetchArgs) { return await adapted_fetch({method: 'GET' , url }) }
export async function UPDATE({url, body}: FetchArgs) { return await adapted_fetch({method: 'PUT' , url, body}) }
export async function DELETE({url }: FetchArgs) { return await adapted_fetch({method: 'DELETE', url }) }

// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options
async function custom_fetch(method, url, body) {
const headers = {
'content-type': 'application/json',
}

// when dealing with FormData, i.e., when uploading files, allow the browser to set the request up
// so boundary information is built properly.
if (body instanceof FormData) {
delete headers['content-type']
} else {
body = JSON.stringify(body)
}

async function adapted_fetch({method, url, body}: AdaptedFetchArgs) {
start(url)
const response = await fetch(url, {

// @ts-expect-error, not sure how to correct this one via typescript
const response: Response = await fetch(url, {
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
method,
headers,
body,
})
.catch (throw_error) // these only occur for network errors, like these:
// * request made with a bad host, e.g., //httpbin
// * the host is refusing connections
// * client is offline, i.e., airplane mode or something
// * CORS preflight failures
.finally(() => stop(url))
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(body),
}).catch(throw_error) // these only occur for network errors, like these:
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
// * request made with a bad host, e.g., //httpbin
// * the host is refusing connections
// * client is offline, i.e., airplane mode or something
// * CORS preflight failures
.finally(() => stop(url))

// reminder: fetch does not throw exceptions for non-200 responses (https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)
if (! response.ok) {
Expand Down
5 changes: 4 additions & 1 deletion next-app/src/lib/forms/Button.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<script>
export let danger = false
export {clazz as class}

let clazz = ''
</script>

<!-- https://daisyui.com/components/button -->
<button on:click class:danger class='btn btn-primary { $$props.class }'>
<button on:click class:danger class='btn btn-primary { clazz }'>
<slot />
</button>

Expand Down
8 changes: 4 additions & 4 deletions next-app/src/lib/forms/Input.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script lang=ts>
import { onMount } from 'svelte'

export let label = ''
Expand All @@ -8,7 +8,7 @@
export let autofocus = false

let id = randomId()
let input = {}
let input: HTMLInputElement

onMount(autofocusIfRequested)

Expand All @@ -17,11 +17,11 @@
}

function autofocusIfRequested() {
autofocus && input.focus()
autofocus && input?.focus()
}

// works around "svelte(invalid-type)" warning, i.e., can't have a dynamic type AND bind:value...keep an eye on https://github.com/sveltejs/svelte/issues/3921
function typeWorkaround(node) {
function typeWorkaround(node: HTMLInputElement) {
node.type = type
}
</script>
Expand Down
12 changes: 6 additions & 6 deletions next-app/src/lib/progress/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { writable } from 'svelte/store'
import { writable, type Writable } from 'svelte/store'

export const loading = writable(false)
export const loading: Writable<boolean> = writable(false)

const pending = []
const pending: Id[] = []

export function start(id) {
export function start(id: Id) {
loading.set(true)

pending.push(id)
}

export function stop(id) {
const i = pending.findIndex(anId => anId === id)
export function stop(id: Id) {
const i = pending.findIndex(_id => _id === id)

if (i >= 0) {
pending.splice(i,1)
Expand Down
1 change: 1 addition & 0 deletions next-app/src/lib/progress/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Id = string | number
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
myieye marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 7 additions & 20 deletions next-app/src/lib/server/sf.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { error } from '@sveltejs/kit'
import type { Rpc, FetchArgs, SfResponse } from './types'

/**
*
* @typedef RPC
* @type {object}
* @property {string} name Name of the remote procedure to call
* @property {string[]} [args] Arguments to pass to the remote procedure
* @property {string} [cookie] https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
*
* @param { RPC } rpc
*/
export async function sf(rpc) {
const { name, args = [], cookie } = rpc

export async function sf({name, args = [], cookie = ''}: Rpc) {
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
const body = {
id: Date.now(),
method: name,
Expand All @@ -21,7 +10,7 @@ export async function sf(rpc) {
},
}

const results = await custom_fetch(`${process.env.API_HOST}/api/sf`, 'post', body, cookie)
const results = await adapted_fetch({url: `${process.env.API_HOST}/api/sf`, method: 'POST', body, cookie})

if (results.error) {
console.log('lib/server/sf.ts.sf results.error: ', {results})
Expand All @@ -36,27 +25,25 @@ export async function sf(rpc) {
return results.result
}

async function custom_fetch(url, method, body, cookie) {
const bodyAsJSON = JSON.stringify(body)

async function adapted_fetch({url, method, body, cookie = ''}: FetchArgs): Promise<SfResponse> {
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options
const response: Response = await fetch(url, {
method,
headers: {
'content-type': 'application/json',
cookie,
},
body: bodyAsJSON,
body: JSON.stringify(body),
}).catch(e => {
// these only occur for network errors, like these:
// request made with a bad host, e.g., //httpbin
// the host is refusing connections
console.log(`lib/server/sf.ts.custom_fetch caught error on ${url}=>${bodyAsJSON}: `, {e})
console.log(`lib/server/sf.ts.adapted_fetch caught error on ${{url, body}}: `, {e})
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
throw error(500, 'NETWORK ERROR with legacy app')
})

if (! response.ok) {
console.log(`lib/server/sf.ts.custom_fetch response !ok ${url}=>${bodyAsJSON}: `, await response.text())
console.log(`lib/server/sf.ts.adapted_fetch response !ok ${{url, body}}: `, await response.text())
throw error(response.status, response.statusText)
}

Expand Down
31 changes: 31 additions & 0 deletions next-app/src/lib/server/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { HttpMethod } from '@sveltejs/kit/types/private'

type Rpc = {
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
name: string,
args?: string[] | object[],
cookie?: string,
}

type FetchArgs = {
url: string,
method: HttpMethod,
body: object,
cookie?: string,
}

type SfResponse = {
error?: {
message: string
},
result?: any,
}

type LegacySession = {
userId: string,
userProjectRole: string,
}

type User = {
id: string,
role: string,
}
5 changes: 3 additions & 2 deletions next-app/src/lib/server/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { error } from '@sveltejs/kit'
import { sf } from '$lib/server/sf'
import type { LegacySession, User } from './types'

export async function fetch_current_user(cookie) {
const { userId, userProjectRole } = await sf({
export async function fetch_current_user(cookie: string): Promise<User> {
const { userId, userProjectRole }: LegacySession = await sf({
name: 'session_getSessionData',
cookie,
})
Expand Down
Loading