Skip to content

Commit

Permalink
Upd for OAK v16
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptographix committed May 6, 2024
1 parent ebd3403 commit 0912f69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
16 changes: 8 additions & 8 deletions packages/pix-server/server/api-pix-server/api-pix-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CobIdentifier, CobListIdentifier, PixApiCobService } from "../../../api
import { MemoryCobStore } from "./memory-cob-data-store.ts";
import { MemoryLocStore } from "./memory-loc-data-store.ts";

import { getBodyJSON, getRouteParams, getQueryParams } from "./helpers.ts";
import { getBodyJSON } from "./helpers.ts";

function convertDate(s: string): Date {
return new Date(s);
Expand All @@ -33,8 +33,8 @@ export function cobRouter(

router
.get(path + "/" + cobType, async (context) => {
let rawParams = getQueryParams(context);
let pag = {
const rawParams = context.params;
const pag = {
paginaAtual: rawParams["paginacao.paginaAtual"],
itensPorPagina: rawParams["paginacao.itensPorPagina"],
}
Expand Down Expand Up @@ -81,7 +81,7 @@ export function cobRouter(
// context.response.body = "Hello world!";
})
.get(path + "/" + cobType + "/:txid", async (context) => {
const { txid } = getRouteParams(context);
const { txid } = context.params;

try {
const id: CobIdentifier = {
Expand All @@ -98,15 +98,15 @@ export function cobRouter(
}
})
.post(path + "/" + cobType + "/:txid", async (context) => {
const body = context.request.body();
const body = context.request.body;

console.log( context.request.headers );
console.log( body.type );
console.log( await (body.value) );
console.log( await (body.text) );
context.response.body = "OK";
})
.put(path + "/" + cobType + "/:txid", async (context) => {
const { txid } = getRouteParams(context);
const { txid } = context.params;
console.log(txid);

const cobIn = await getBodyJSON<Cobranca>(context);
Expand All @@ -126,7 +126,7 @@ export function cobRouter(
}
})
.patch(path + "/" + cobType + "/:txid", async (context) => {
const { txid } = getRouteParams(context);
const { txid } = context.params;
console.log(txid);

const cobIn = await getBodyJSON<Cobranca>(context);
Expand Down
13 changes: 5 additions & 8 deletions packages/pix-server/server/api-pix-server/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { RouterContext, helpers } from "https://deno.land/x/oak/mod.ts";
import { RouterContext } from "https://deno.land/x/oak/mod.ts";

const { getQuery } = helpers;
export const getQueryParams = getQuery;

export function getRouteParams<A>( context: RouterContext ): A {
export function getRouteParams<A,S extends string>( context: RouterContext<S> ): A {
const params = context.params ?? {};

return params as unknown as A;
}

export async function getBodyJSON<A=any>( context: RouterContext ): Promise<A> {
export async function getBodyJSON<A=any, S extends string=string>( context: RouterContext<S> ): Promise<A> {
if ( context.request.hasBody ) {
const body = context.request.body( {type: 'json' });
const body = context.request.body.json();

return (await body.value) as unknown as A;
return (await body) as unknown as A;
}
else {
return null as unknown as A;
Expand Down

0 comments on commit 0912f69

Please sign in to comment.