headers personalizados #3 #4

Open
fauno wants to merge 3 commits from issue-3 into antifascista
12 changed files with 82 additions and 80 deletions

View file

@ -78,7 +78,7 @@ export interface CustomerResponse {
export async function getCustomers(
host: string,
token: string,
headers: HeadersInit,
options: CustomerQuery
): Promise<CustomerResponse> {
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);

View file

@ -15,17 +15,29 @@ import {
getPriceLists,
} from "../index.js";
// TODO: hacerlo input
const HOST = "http://sutty.vm:4001";
const hostInput = document.querySelector<HTMLInputElement>("#host")!;
const userInput = document.querySelector<HTMLInputElement>("#user")!;
const passInput = document.querySelector<HTMLInputElement>("#pass")!;
const tokenInput = document.querySelector<HTMLInputElement>("#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<any>) {
});
}
setupForm("#token", () => dummy(HOST, token()));
setupForm("#save-token", async () => {
localStorage.token = token();
setupForm("#token", () => {
console.log(headers());
Review

Borrar console.lo

Borrar console.lo
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(), {
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);
});

View file

@ -31,6 +31,15 @@
}
</style>
<label for="host">Host: </label>
<input id="host" name="host" value="http://sutty.vm:4001" />
<label for="user">User: </label>
<input id="user" name="user" />
<label for="pass">Pass: </label>
<input id="pass" name="pass" value="" />
Review

Aclarar que es especifico para nuestro sistema

Aclarar que es especifico para nuestro sistema
Review

bueno pero igual es una demostración de que podés customizar los headers

bueno pero igual es una demostración de que podés customizar los headers
Review

No digo que lo saques, solo aclarar en la demo para que no sea confuso.

No digo que lo saques, solo aclarar en la demo para que no sea confuso.
<label for="token-input">Access token: </label>
<input id="token-input" name="token" />
@ -56,7 +65,11 @@
<form id="price">
<button>Conseguir precios</button>
<input name="filter" type="text" placeholder="Filtro por lista de precios (ID, opcional)" />
<input
name="filter"
type="text"
placeholder="Filtro por lista de precios (ID, opcional)"
/>
<p class="status"></p>
</form>
@ -75,7 +88,11 @@
<form id="stock">
<button>Conseguir stock</button>
<input name="filter" type="text" placeholder="Filtro por ID (opcional)" />
<input name="warehouseCode" type="text" placeholder="Filtro por código de deposito (opcional)" />
<input
name="warehouseCode"
type="text"
placeholder="Filtro por código de deposito (opcional)"
/>
<p class="status"></p>
</form>

View file

@ -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) {

View file

@ -114,15 +114,12 @@ export interface OrderResponse {}
export async function order(
host: string,
token: string,
headers: HeadersInit,
order: OrderDto
): Promise<OrderResponse> {
const res = await fetch(`${host}/api/Aperture/order`, {
method: "POST",
headers: {
accesstoken: token,
"content-type": "application/json",
Review

Se deberia aplicar el content-type siempre si lo requiere el endpoint

Se deberia aplicar el content-type siempre si lo requiere el endpoint
Review

en el sitio le puse que siempre esté, no sé por qué en este endpoint específico se agrega y no en otros

en el sitio le puse que siempre esté, no sé por qué en este endpoint específico se agrega y no en otros
Review

A mi entender es porque si se le pasa cosas por el body, en el resto no

A mi entender es porque si se le pasa cosas por el body, en el resto no
},
headers,
body: JSON.stringify(order),
});
const json = await res.json();

View file

@ -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": {

View file

@ -24,7 +24,7 @@ export interface PriceResponse {
export async function getPrices(
host: string,
token: string,
headers: HeadersInit,
options: PriceQuery
): Promise<PriceResponse> {
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);

View file

@ -28,7 +28,7 @@ export interface PreciosResponse {
export async function getPricesByCustomer(
host: string,
token: string,
headers: HeadersInit,
options: PriceByCustomerQuery
): Promise<PreciosResponse> {
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);

View file

@ -23,7 +23,7 @@ export interface PriceListResponse {
export async function getPriceLists(
host: string,
token: string,
headers: HeadersInit,
options: PriceListQuery
): Promise<PriceListResponse> {
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);

View file

@ -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<ProductosResponse> {
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);

View file

@ -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<PublicationsResponse> {
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);

View file

@ -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<StockResponse> {
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);