Skip to content

Commit

Permalink
chore: use json instead of form data
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 2, 2024
1 parent 5a2ce45 commit fda75ef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
28 changes: 18 additions & 10 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,22 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
return c.NoContent(http.StatusUnauthorized)
}

name := c.FormValue("name")
var requestData api.CreateAppRequest
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Error: true,
Message: fmt.Sprintf("Bad request: %s", err.Error()),
})
}

name := requestData.Name
var pairingPublicKey string
var pairingSecretKey string
if c.FormValue("pubkey") == "" {
if requestData.Pubkey == "" {
pairingSecretKey = nostr.GeneratePrivateKey()
pairingPublicKey, _ = nostr.GetPublicKey(pairingSecretKey)
} else {
pairingPublicKey = c.FormValue("pubkey")
pairingPublicKey = requestData.Pubkey
//validate public key
decoded, err := hex.DecodeString(pairingPublicKey)
if err != nil || len(decoded) != 32 {
Expand All @@ -198,12 +206,12 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
}
}
app := App{Name: name, NostrPubkey: pairingPublicKey}
maxAmount, _ := strconv.Atoi(c.FormValue("maxAmount"))
budgetRenewal := c.FormValue("budgetRenewal")
maxAmount, _ := strconv.Atoi(requestData.MaxAmount)
budgetRenewal := requestData.BudgetRenewal

expiresAt := time.Time{}
if c.FormValue("expiresAt") != "" {
expiresAt, err = time.Parse(time.RFC3339, c.FormValue("expiresAt"))
if requestData.ExpiresAt != "" {
expiresAt, err = time.Parse(time.RFC3339, requestData.ExpiresAt)
if err != nil {
svc.Logger.Errorf("Invalid expiresAt: %s", pairingPublicKey)
return c.JSON(http.StatusBadRequest, ErrorResponse{
Expand All @@ -223,7 +231,7 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
return err
}

requestMethods := c.FormValue("requestMethods")
requestMethods := requestData.RequestMethods
if requestMethods == "" {
return fmt.Errorf("Won't create an app without request methods.")
}
Expand Down Expand Up @@ -272,8 +280,8 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
responseBody.Pubkey = pairingPublicKey
responseBody.PairingSecret = pairingSecretKey

if c.FormValue("returnTo") != "" {
returnToUrl, err := url.Parse(c.FormValue("returnTo"))
if requestData.ReturnTo != "" {
returnToUrl, err := url.Parse(requestData.ReturnTo)
if err == nil {
query := returnToUrl.Query()
query.Add("relay", publicRelayUrl)
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/QRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type Props = {

function QRCode({ value, size, level, className }: Props) {
const isDarkMode = useDarkMode();
console.log(isDarkMode);
const fgColor = isDarkMode ? "#FFFFFF" : "#242424";
const bgColor = isDarkMode ? "#242424" : "#FFFFFF";

Expand Down
17 changes: 3 additions & 14 deletions frontend/src/screens/apps/NewApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,22 @@ const NewApp = () => {
throw new Error("No CSRF token");
}

const formData = new FormData();
formData.append("name", appName);
formData.append("maxAmount", maxAmount.toString());
formData.append("budgetRenewal", budgetRenewal);
formData.append("expiresAt", expiresAt);
formData.append("requestMethods", requestMethods);
formData.append("pubkey", pubkey);
formData.append("returnTo", returnTo);

try {
const response = await fetch("/api/apps", {
method: "POST",
headers: {
"X-CSRF-Token": csrf,
"Content-Type": "application/json",
},
body: formData,

// TODO: send consider sending JSON data
/*body: JSON.stringify({
body: JSON.stringify({
name: appName,
pubkey: pubkey,
maxAmount: maxAmount.toString(),
budgetRenewal: budgetRenewal,
expiresAt: expiresAt,
requestMethods: requestMethods,
returnTo: returnTo,
}),*/
}),
});
await validateFetchResponse(response);

Expand Down
10 changes: 10 additions & 0 deletions models/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ type ListAppsResponse struct {
Apps []App `json:"apps"`
}

type CreateAppRequest struct {
Name string `json:"name"`
Pubkey string `json:"pubkey"`
MaxAmount string `json:"maxAmount"`
BudgetRenewal string `json:"budgetRenewal"`
ExpiresAt string `json:"expiresAt"`
RequestMethods string `json:"requestMethods"`
ReturnTo string `json:"returnTo"`
}

type CreateAppResponse struct {
PairingUri string `json:"pairingUri"`
PairingSecret string `json:"pairingSecretKey"`
Expand Down

0 comments on commit fda75ef

Please sign in to comment.