examples icon indicating copy to clipboard operation
examples copied to clipboard

Adding API Client example

Open dalanmiller opened this issue 1 year ago • 2 comments

Hi there Hono team!

I wanted to add an example to help others through the issue I encountered and discussed over here: https://github.com/orgs/honojs/discussions/2552

I realized I was trying to bring something from Flask into Hono which is their before_request & after_request decorators which pretty much run them when their name suggests.

https://flask.palletsprojects.com/en/3.0.x/api/#flask.Flask.before_request https://flask.palletsprojects.com/en/3.0.x/api/#flask.Flask.after_request

Where I got stuck though and what some of the helpful replies on the discussion got me to was the necessary extensions of Variables and Bindings when instantiating the Hono() app.

Hopefully this example makes this clear for the next person!

dalanmiller avatar Apr 27 '24 08:04 dalanmiller

Hi @dalanmiller

Awesome! This is a good example of how to use Bindings and Variables and use c.set/c.var. It looks good to me.

I'd like the Stripe Developer Advocate @hideokamoto to review this one!

yusukebe avatar Apr 29 '24 03:04 yusukebe

Thank you @yusukebe!

Hidetaka - I'll message you on Slack. 🙇🏻‍♂️

dalanmiller avatar Apr 29 '24 04:04 dalanmiller

I apologize for the delay in confirming! I think this is a very simple yet great sample for understanding Hono's capabilities.

@dalanmiller I have one question about this sample. It seems that both Live and Test API keys for Stripe need to be set. Since this is a sample project, would it be okay to simplify it to just one environment variable, say STRIPE_API_KEY? I thought that if we were to use it in production, we could simply set the Live secret key to this variable.

hideokamoto-stripe avatar May 08 '24 11:05 hideokamoto-stripe

@yusukebe Also, regarding the Hono and Stripe sample, I have a suggestion. How about creating a pull request to this repository with a very simple e-commerce app sample using Hono & Stripe? After looking at this sample, I thought it could serve as a demo for SPAs/MPAs built with Hono, combining Cloudflare's KV or D1.

If it's okay to request a second integration sample with Stripe, I will go ahead and build it based on this demo.

hideokamoto-stripe avatar May 08 '24 11:05 hideokamoto-stripe

Hi @hideokamoto-stripe

Also, regarding the Hono and Stripe sample, I have a suggestion. How about creating a pull request to this repository with a very simple e-commerce app sample using Hono & Stripe? After looking at this sample, I thought it could serve as a demo for SPAs/MPAs built with Hono, combining Cloudflare's KV or D1.

This will be a super exciting project! But I'm considering we don't have to include the "e-commerce app sample" in this repo because perhaps it will be hard to maintain it (though this PR project is okay because it is so tiny).

What about you creating it for your personal project on GitHub and creating a link to it from the official Hono website?

yusukebe avatar May 09 '24 09:05 yusukebe

I have one question about this sample. It seems that both Live and Test API keys for Stripe need to be set. Since this is a sample project, would it be okay to simplify it to just one environment variable, say STRIPE_API_KEY? I thought that if we were to use it in production, we could simply set the Live secret key to this variable.

Sounds good @hideokamoto-stripe - I chose TEST and SECRET as it was the exact scenario that I had to solve for and didn't have an example to show me. For the purposes of a Tap to Pay Terminal client I wanted to be able to select livemode or testmode and have a single Cloudflare worker backing it.

But, I will simplify it down to just one environment variable per your suggestion.

dalanmiller avatar May 12 '24 05:05 dalanmiller

Hi @dalanmiller

Is this ready to be merged?

yusukebe avatar May 15 '24 21:05 yusukebe

IMO It looks ready to merge. We can discuss how we will import the Stripe API key, but the most important point is showing a new example about how to use the c.set and c.var.

@dalanmiller Is it okay to merge this PR? After that, I'm going to make a new PR to discuss about the way how we can handle the Stripe API key.

hideokamoto-stripe avatar May 15 '24 23:05 hideokamoto-stripe

@dalanmiller @hideokamoto-stripe

One thing we have to consider again is whether the project name api-client is good or not. When I saw the name api-client, I imagined "HTTP Client" by the hc feature. So it may not be a proper name. But I don't have a good name now. Do you have any thoughts? Or, simply make the name stripe, though the taste is different from that of other projects.

yusukebe avatar May 16 '24 21:05 yusukebe

Now having another thought, the main purpose of the example is to show how to access environment variables in Typescript. The fact that the env var is an API key is secondary/irrelevant.

I think env-vars or env-vars-[typescript-ts]?

dalanmiller avatar May 16 '24 21:05 dalanmiller

Now having another thought, the main purpose of the example is to show how to access environment variables in Typescript. The fact that the env var is an API key is secondary/irrelevant.

Indeed, you are right!

I think env-vars or env-vars-[typescript-ts]?

I like env-vars! Shall we go with it?

yusukebe avatar May 16 '24 21:05 yusukebe

Ready for review again @yusukebe 🕵🏻‍♂️

dalanmiller avatar May 17 '24 11:05 dalanmiller

@dalanmiller

Oops. It has a conflict. Can you resolve it?

yusukebe avatar May 18 '24 00:05 yusukebe

Good now @yusukebe

dalanmiller avatar May 18 '24 03:05 dalanmiller

@dalanmiller Thanks! Mergin now.

yusukebe avatar May 20 '24 21:05 yusukebe