From 3eafdcb7dd7ca1f8311483b3f451503d8509ee8e Mon Sep 17 00:00:00 2001 From: f Date: Tue, 1 Nov 2022 14:21:52 -0300 Subject: [PATCH 1/3] feat: usar headers personalizados #3 --- customer.ts | 8 ++------ dummy.ts | 6 ++---- order.ts | 7 ++----- price.ts | 8 ++------ priceByCustomer.ts | 8 ++------ priceList.ts | 8 ++------ product.ts | 8 ++------ publications.ts | 8 ++------ stock.ts | 8 ++------ 9 files changed, 18 insertions(+), 51 deletions(-) diff --git a/customer.ts b/customer.ts index a619ddb..5798636 100644 --- a/customer.ts +++ b/customer.ts @@ -78,7 +78,7 @@ export interface CustomerResponse { export async function getCustomers( host: string, - token: string, + headers: HeadersInit, options: CustomerQuery ): Promise { let searchParams = new URLSearchParams(); @@ -88,11 +88,7 @@ export async function getCustomers( } const res = await fetch( `${host}/api/Aperture/Customer?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/dummy.ts b/dummy.ts index d8bcbcd..97e616b 100644 --- a/dummy.ts +++ b/dummy.ts @@ -1,9 +1,7 @@ -export async function dummy(host: string, token: string) { +export async function dummy(host: string, headers: HeadersInit) { const res = await fetch(`${host}/api/Aperture/dummy`, { method: "POST", - headers: { - accesstoken: token, - }, + headers, }); const json = await res.json(); if (!json.isOk) { diff --git a/order.ts b/order.ts index 47294a2..e6fd963 100644 --- a/order.ts +++ b/order.ts @@ -114,15 +114,12 @@ export interface OrderResponse {} export async function order( host: string, - token: string, + headers: HeadersInit, order: OrderDto ): Promise { const res = await fetch(`${host}/api/Aperture/order`, { method: "POST", - headers: { - accesstoken: token, - "content-type": "application/json", - }, + headers, body: JSON.stringify(order), }); const json = await res.json(); diff --git a/price.ts b/price.ts index aa744ee..b677a4f 100644 --- a/price.ts +++ b/price.ts @@ -24,7 +24,7 @@ export interface PriceResponse { export async function getPrices( host: string, - token: string, + headers: HeadersInit, options: PriceQuery ): Promise { let searchParams = new URLSearchParams(); @@ -37,11 +37,7 @@ export async function getPrices( } const res = await fetch( `${host}/api/Aperture/Price?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/priceByCustomer.ts b/priceByCustomer.ts index d708039..fc2d0da 100644 --- a/priceByCustomer.ts +++ b/priceByCustomer.ts @@ -28,7 +28,7 @@ export interface PreciosResponse { export async function getPricesByCustomer( host: string, - token: string, + headers: HeadersInit, options: PriceByCustomerQuery ): Promise { let searchParams = new URLSearchParams(); @@ -44,11 +44,7 @@ export async function getPricesByCustomer( } const res = await fetch( `${host}/api/Aperture/PriceByCustomer?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/priceList.ts b/priceList.ts index 4dedb9b..485e3a6 100644 --- a/priceList.ts +++ b/priceList.ts @@ -23,7 +23,7 @@ export interface PriceListResponse { export async function getPriceLists( host: string, - token: string, + headers: HeadersInit, options: PriceListQuery ): Promise { let searchParams = new URLSearchParams(); @@ -33,11 +33,7 @@ export async function getPriceLists( } const res = await fetch( `${host}/api/Aperture/PriceList?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/product.ts b/product.ts index 3062be0..6d6da5f 100644 --- a/product.ts +++ b/product.ts @@ -50,7 +50,7 @@ export interface ProductosResponse { // https://github.com/TangoSoftware/ApiTiendas#art%C3%ADculos export async function getProductos( host: string, - token: string, + headers: HeadersInit, options: ProductosQuery ): Promise { let searchParams = new URLSearchParams(); @@ -63,11 +63,7 @@ export async function getProductos( } const res = await fetch( `${host}/api/Aperture/Product?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/publications.ts b/publications.ts index 031d73c..7ab1ac9 100644 --- a/publications.ts +++ b/publications.ts @@ -23,7 +23,7 @@ export interface PublicationsResponse { // https://github.com/TangoSoftware/ApiTiendas#art%C3%ADculos export async function getPublications( host: string, - token: string, + headers: HeadersInit, options: PublicationsQuery ): Promise { let searchParams = new URLSearchParams(); @@ -39,11 +39,7 @@ export async function getPublications( } const res = await fetch( `${host}/api/Aperture/Publications?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); diff --git a/stock.ts b/stock.ts index 32e87fa..5afc976 100644 --- a/stock.ts +++ b/stock.ts @@ -23,7 +23,7 @@ export interface StockResponse { // https://github.com/TangoSoftware/ApiTiendas#saldos-de-stock export async function getStocks( host: string, - token: string, + headers: HeadersInit, options: StockQuery ): Promise { let searchParams = new URLSearchParams(); @@ -36,11 +36,7 @@ export async function getStocks( } const res = await fetch( `${host}/api/Aperture/Stock?${searchParams.toString()}`, - { - headers: { - accesstoken: token, - }, - } + { headers } ); const json = await res.json(); console.debug(json); -- 2.45.2 From 6e4a9a74bdf39779f36db717d5b1c3ff1fba83ab Mon Sep 17 00:00:00 2001 From: f Date: Tue, 1 Nov 2022 14:23:48 -0300 Subject: [PATCH 2/3] feat: la demo permite usar basic auth --- demo/demo.ts | 70 +++++++++++++++++++++++++++++++------------------ demo/index.html | 21 +++++++++++++-- 2 files changed, 63 insertions(+), 28 deletions(-) diff --git a/demo/demo.ts b/demo/demo.ts index 0ac21b7..423fd35 100644 --- a/demo/demo.ts +++ b/demo/demo.ts @@ -15,17 +15,29 @@ import { getPriceLists, } from "../index.js"; -// TODO: hacerlo input -const HOST = "http://sutty.vm:4001"; +const hostInput = document.querySelector("#host")!; +const userInput = document.querySelector("#user")!; +const passInput = document.querySelector("#pass")!; const tokenInput = document.querySelector("#token-input")!; -if (localStorage.token) { - tokenInput.value = localStorage.token; +function headers() { + const h = new Headers(); + + h.set("content-type", "application/json"); + + if (tokenInput.value) { + h.set("accesstoken", tokenInput.value); + } else { + const basic = btoa(`${userInput.value}:${passInput.value}`); + h.set("authorization", `Basic ${basic}`); + } + + return h; } -function token() { - return tokenInput.value; +function host() { + return hostInput.value; } function clear(el: Node) { @@ -88,34 +100,34 @@ function setupForm(selector: string, listener: (event: Event) => Promise) { }); } -setupForm("#token", () => dummy(HOST, token())); -setupForm("#save-token", async () => { - localStorage.token = token(); +setupForm("#token", () => { + console.log(headers()); + return dummy(host(), headers()); }); -setupForm("#productos", () => getProductos(HOST, token(), {})); -setupForm("#publicaciones", () => getPublications(HOST, token(), {})); +setupForm("#productos", () => getProductos(host(), headers(), {})); +setupForm("#publicaciones", () => getPublications(host(), headers(), {})); setupForm("#price", (event) => - getPrices(HOST, token(), { + getPrices(host(), headers(), { filter: (event.target! as any).filter.value, }) ); setupForm("#stock", (event) => - getStocks(HOST, token(), { + getStocks(host(), headers(), { filter: (event.target! as any).filter.value, warehouseCode: (event.target! as any).warehouseCode.value, }) ); setupForm("#price-by-customer", (event) => - getPricesByCustomer(HOST, token(), { + getPricesByCustomer(host(), headers(), { filter: (event.target! as any).filter.value, }) ); setupForm("#price-list", (event) => - getPriceLists(HOST, token(), { + getPriceLists(host(), headers(), { filter: (event.target! as any).filter.value, }) ); -setupForm("#customers", () => getCustomers(HOST, token(), {})); +setupForm("#customers", () => getCustomers(host(), headers(), {})); let customer: Customer | null = null; @@ -123,7 +135,7 @@ setupForm("#pedido-customer", async (event) => { const cuit = (event.target! as any).cuit.value; if (cuit.length === 0) throw new Error("No pusiste un CUIT."); - const customers = await getCustomers(HOST, token(), { + const customers = await getCustomers(host(), headers(), { customer: { type: TipoDeDocumento.CUIT, number: cuit, @@ -160,17 +172,23 @@ setupForm("#pedido-item", async (event) => { const sku = (event.target! as any).sku.value; if (sku.length === 0) throw new Error("No pusiste un SKU."); - const productosResponse = await getProductos(HOST, token(), { filter: sku }); + const productosResponse = await getProductos(host(), headers(), { + filter: sku, + }); if (productosResponse.Data.length !== 1) throw new Error("Encontré más de un producto."); - const preciosDelClienteResponse = await getPricesByCustomer(HOST, token(), { - SKUCode: sku, - customer: { - type: customer.DocumentType, - number: customer.DocumentNumber, - }, - }); + const preciosDelClienteResponse = await getPricesByCustomer( + host(), + headers(), + { + SKUCode: sku, + customer: { + type: customer.DocumentType, + number: customer.DocumentNumber, + }, + } + ); if (preciosDelClienteResponse.Data.length !== 1) throw new Error("Encontré más de un producto."); @@ -216,5 +234,5 @@ setupForm("#pedido", async () => { }, }; - return await order(HOST, token(), orderJson); + return await order(host(), headers(), orderJson); }); diff --git a/demo/index.html b/demo/index.html index 0295284..1aa71e4 100644 --- a/demo/index.html +++ b/demo/index.html @@ -31,6 +31,15 @@ } + + + + + + + + + @@ -56,7 +65,11 @@
- +

@@ -75,7 +88,11 @@
- +

-- 2.45.2 From 55f53cb9a0d508ee6097ed52074f5111206a2c2b Mon Sep 17 00:00:00 2001 From: f Date: Tue, 1 Nov 2022 14:28:26 -0300 Subject: [PATCH 3/3] 0.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d395a8e..913df26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@suttyweb/hyperpop", - "version": "0.3.0", + "version": "0.4.0", "description": "Un cliente de API de https://github.com/TangoSoftware/ApiTiendas", "license": "SEE LICENSE IN LICENSE", "scripts": { -- 2.45.2