Jing
Jing
HHono
Created by Jing on 7/3/2024 in #help
Hono JS best practice for DRY
What are the best practices in Hono for writing APIs, specifically when one API (API B) partially replicates the functionality of another API (API A)? For example, if API A performs a specific task, and API B performs a different task but includes a part that is identical to the task in API A, what is the recommended approach to handle this overlap efficiently and maintainably?
5 replies
HHono
Created by Jing on 6/29/2024 in #help
Synchronous function returns no value unless await
Hello everyone, I'm encountering a confusing issue while working with Hono.js. In my project, I have a function named getEbayAuthUrl which is not defined as an async function and simply returns a string, e.g., "hi". However, I've noticed that if I do not use await when calling this function, the value doesn't seem to be processed correctly, as if it were missing in subsequent database operations. Here's a simplified version of the function:
function getEbayAuthUrl() {
return "MyAuthUrl";
}

const app = new Hono();

app.post(
'/',
verifyAuth(),
zValidator(
'json',
insertOauthCredentialSchema.pick({
clientId: true,
clientSecret: true
}),
),
async (c) => {
const values = c.req.valid('json');

const authUrl = await getEbayAuthUrl(values);
const inserted = await db
.insert('oauthCredentials')
.values({
authorizationUrl: authUrl,
...values,
})
.returning('*');

return c.json({ data: inserted });
},
);

export default app;
function getEbayAuthUrl() {
return "MyAuthUrl";
}

const app = new Hono();

app.post(
'/',
verifyAuth(),
zValidator(
'json',
insertOauthCredentialSchema.pick({
clientId: true,
clientSecret: true
}),
),
async (c) => {
const values = c.req.valid('json');

const authUrl = await getEbayAuthUrl(values);
const inserted = await db
.insert('oauthCredentials')
.values({
authorizationUrl: authUrl,
...values,
})
.returning('*');

return c.json({ data: inserted });
},
);

export default app;
It doesn’t work as expected, and it seems like the value isn't recognized in subsequent database operations.
const authUrl = getEbayAuthUrl(values);

# Shows {} in database
const authUrl = getEbayAuthUrl(values);

# Shows {} in database
VS.
const authUrl = await getEbayAuthUrl(values);

# Shows "MyAuthUrl" in database
const authUrl = await getEbayAuthUrl(values);

# Shows "MyAuthUrl" in database
I thought await was only necessary for functions returning Promises. Does anyone know why I would need to use await here even though the function doesn’t return a Promise? Is this something specific to Hono.js, or is there a general JS principle I’m missing?
6 replies