Skip to content

Commit

Permalink
typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkuz committed Jul 21, 2023
1 parent 7fe6544 commit 562c57a
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 994 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"env": {
"browser": true,
"es2021": true
"es2021": true,
"node": true
},
"extends": "airbnb-base",
"extends": "standard-with-typescript",
"overrides": [
],
"parserOptions": {
Expand Down
80 changes: 0 additions & 80 deletions build.js

This file was deleted.

10 changes: 10 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export declare function Get(endpoint: string, header?: object | null, optionalApiAddress?: string | null): Promise<any>;
export declare function Post(endpoint: string, body: object | string | null, header?: object | null, optionalApiAddress?: object | null): Promise<any>;
export declare function Patch(endpoint: string, body: object | string | null, header?: null, optionalApiAddress?: null): Promise<any>;
export declare function Put(endpoint: string, body: object | string | null, header?: null, optionalApiAddress?: null): Promise<any>;
export declare function Delete(endpoint: string, body: object | string | null, header?: null, optionalApiAddress?: null): Promise<any>;
declare function Configurate(api?: string, getAuthorization?: () => string): void;
declare const _default: {
Configurate: typeof Configurate;
};
export default _default;
59 changes: 59 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Delete = exports.Put = exports.Patch = exports.Post = exports.Get = void 0;
const config = {
api: '',
getAuthorization: () => '',
};
function HeaderBuilder(header, body = null) {
const headers = {
...header,
...(body ? { 'Content-Type': 'application/json' } : null),
...(config.getAuthorization() ? { authorization: config.getAuthorization() } : null),
};
return headers;
}
async function Handler(res) {
const json = await res.json();
if (!res.ok) {
throw json;
}
else {
return json;
}
}
async function Get(endpoint, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;
return fetch(address, { method: 'GET', headers: HeaderBuilder(header) })
.then(Handler);
}
exports.Get = Get;
async function Post(endpoint, body, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;
return fetch(address, { method: 'POST', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}
exports.Post = Post;
async function Patch(endpoint, body, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;
return fetch(address, { method: 'PATCH', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}
exports.Patch = Patch;
async function Put(endpoint, body, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;
return fetch(address, { method: 'PUT', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}
exports.Put = Put;
async function Delete(endpoint, body, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;
return fetch(address, { method: 'DELETE', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}
exports.Delete = Delete;
function Configurate(api = '', getAuthorization = () => '') {
config.api = api;
config.getAuthorization = getAuthorization;
}
exports.default = { Configurate };
23 changes: 14 additions & 9 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const config = {
interface Config {
api: string;
getAuthorization: () => string;
}

const config: Config = {
api: '',
getAuthorization: () => '',
};

function HeaderBuilder(header, body) {
function HeaderBuilder(header: object | null, body: object | string | null = null) : HeadersInit {
const headers = {
...header,
...(body ? { 'Content-Type': 'application/json' } : null),
Expand All @@ -13,7 +18,7 @@ function HeaderBuilder(header, body) {
return headers;
}

async function Handler(res) {
async function Handler(res: Response) {
const json = await res.json();

if (!res.ok) {
Expand All @@ -23,42 +28,42 @@ async function Handler(res) {
}
}

export async function Get(endpoint, header = null, optionalApiAddress = null) {
export async function Get(endpoint: string, header: object | null = null, optionalApiAddress: string | null = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;

return fetch(address, { method: 'GET', headers: HeaderBuilder(header) })
.then(Handler);
}

export async function Post(endpoint, body, header = null, optionalApiAddress = null) {
export async function Post(endpoint: string, body: object | string | null, header: object | null = null, optionalApiAddress: object | null = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;

return fetch(address, { method: 'POST', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}

export async function Patch(endpoint, body, header = null, optionalApiAddress = null) {
export async function Patch(endpoint: string, body: object | string | null, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;

return fetch(address, { method: 'PATCH', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}

export async function Put(endpoint, body, header = null, optionalApiAddress = null) {
export async function Put(endpoint: string, body: object | string | null, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;

return fetch(address, { method: 'PUT', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}

export async function Delete(endpoint, body, header = null, optionalApiAddress = null) {
export async function Delete(endpoint: string, body: object | string | null, header = null, optionalApiAddress = null) {
const address = `${optionalApiAddress || config.api}${endpoint}`;

return fetch(address, { method: 'DELETE', body: JSON.stringify(body), headers: HeaderBuilder(header, body) })
.then(Handler);
}

function Configurate(api = '', getAuthorization = () => '') {
function Configurate(api: string = '', getAuthorization: () => string = () => '') {
config.api = api;
config.getAuthorization = getAuthorization;
}
Expand Down
Loading

0 comments on commit 562c57a

Please sign in to comment.