Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"env": {
"browser": true,
"es2020": true,
"node": true
},
"extends": [
"airbnb-base"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
"env": {
"browser": true,
"es2020": true,
"node": true
},
"extends": ["airbnb-base"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"lines-between-class-members": "off",
"no-tabs": "off",
"indent": ["error", "tab"],
"max-len": ["error", 120],
"implicit-arrow-linebreak": "off",
"function-paren-newline": "off",
"object-curly-newline": "off"
}
}
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"arrowParens": "always",
"bracketSameLine": true,
"bracketSpacing": true,
"endOfLine": "lf",
"printWidth": 120,
"semi": true,
"singleQuote": true,
"useTabs": true,
"tabWidth": 4,
"trailingComma": "all"
}
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# axios-digest
[![Build Status](https://travis-ci.com/rearn/axios-digest.svg?branch=master)](https://travis-ci.com/rearn/axios-digest)

`axios-digest` is axios add digest auth.

## Installation

``` sh
```sh
npm install --save axios-digest
```

## Usage

See test code. (`index.test.ts`)
It pretty much is a wrapper around Axios. the primary or most commonly-used HTTP methods only are available -> `POST, PUT, PATCH, GET, DELETE, HEAD`. See below for usage upfront (It was inspired from the test file).

### AxiosDigest Constructor AxiosDigest(username: string, passwd: string, customAxios: AxiosInstance|AxiosStatic)

### Parameters

- `username`: Not optional | `string`.
- `password`: Not optional | `string`.
- `customAxios`: Optional. An existing axios instance | `AxiosInstance|AxiosStatic`.
- `username`: Not optional | `string`.
- `password`: Not optional | `string`.
- `customAxios`: Optional. An existing axios instance | `AxiosInstance|AxiosStatic`.

```js
import AxiosDigest from '.';
Expand All @@ -31,18 +31,17 @@ const base = 'http://localhost';

const axiosDigest = new AxiosDigest(username, passwd);
// Go ahead and make them request!

```

### Fields && Methods

#### axiosDigest.info

Interface for setting the username && password beyond the `constructor`. It does not include a custom Axios instance as in the constructor.
It receives an object and the fields `username` && `passwd` are not Optional, and returns the same, only that the value for field `passwd` is masked.
It receives an object and the fields `username` && `passwd` are not Optional, and returns the same, only that the value for field `passwd` is masked.

```js
axiosDigest.info = {username, passwd};
axiosDigest.info = { username, passwd };
const info = axiosDigest.info; // { username: '[username]', passwd: '***' }
```

Expand All @@ -52,10 +51,9 @@ The HTTP Methods available have been previously highlighted and returns a Promis

### Similar Parameters

- `path`: Not optional | `string`.
- `data`: Optional | `any`.
- `config`: Optional | `AxiosRequestConfig`.

- `path`: Not optional | `string`.
- `data`: Optional | `any`.
- `config`: Optional | `AxiosRequestConfig`.

#### axiosDigest.head

Expand All @@ -64,23 +62,23 @@ Makes a `HEAD` request.
```js
axiosDigest.head(path: string, config?: AxiosRequestConfig): Promise<any>;
```

#### axiosDigest.delete

Makes a `DELETE` request.

```js
axiosDigest.delete(path: string, config?: AxiosRequestConfig): Promise<any>;
```

#### axiosDigest.get

Makes a `GET` request.

```js
axiosDigest.get(path: string, config?: AxiosRequestConfig): Promise<any>;
```

#### axiosDigest.patch

Makes a `PATCH` request.
Expand All @@ -96,7 +94,7 @@ Makes a `PUT` request.
```js
axiosDigest.put(path: string, data: any, config?: AxiosRequestConfig): Promise<any>;
```

#### axiosDigest.post

Makes a `POST` request.
Expand Down
39 changes: 19 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/* eslint-disable no-unused-vars */
import { AxiosInstance, AxiosRequestConfig, AxiosStatic } from 'axios';

export default class AxiosDigest {
private readonly axios;
private username;
private passwd;
constructor(username: string, passwd: string, customAxios?: AxiosInstance | AxiosStatic);
set info(d: {
username: string;
passwd: string;
});
get info(): {
username: string;
passwd: string;
};
get(path: string, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
post(path: string, data?: any, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
put(path: string, data?: any, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
delete(path: string, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
head(path: string, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
patch(path: string, data?: any, config?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<any, any>>;
private getWwwAuth;
private getAuthHeader;
private readonly axios;
private username;
private passwd;
constructor(username: string, passwd: string, customAxios?: AxiosInstance | AxiosStatic);
set info(d: { username: string; passwd: string });
get info(): {
username: string;
passwd: string;
};
get(path: string, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
post(path: string, data?: any, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
put(path: string, data?: any, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
delete(path: string, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
head(path: string, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
patch(path: string, data?: any, config?: AxiosRequestConfig): Promise<import('axios').AxiosResponse<any, any>>;
private getWwwAuth;
private getAuthHeader;
}
80 changes: 40 additions & 40 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ const base = 'http://localhost';

const axios = new AxiosDigest(username, passwd);
describe('digest', () => {
const url = `/digest-auth/auth/${username}/${passwd}/`;
test('MD5', async () => {
const a = await axios.get(`${base}${url}MD5`);
expect(a.status).toBe(200);
});
test('SHA-256', async () => {
const a = await axios.get(`${base}${url}SHA-256`);
expect(a.status).toBe(200);
});
test('SHA-512', async () => {
const a = await axios.get(`${base}${url}SHA-512`);
expect(a.status).toBe(200);
});
const url = `/digest-auth/auth/${username}/${passwd}/`;
test('MD5', async () => {
const a = await axios.get(`${base}${url}MD5`);
expect(a.status).toBe(200);
});
test('SHA-256', async () => {
const a = await axios.get(`${base}${url}SHA-256`);
expect(a.status).toBe(200);
});
test('SHA-512', async () => {
const a = await axios.get(`${base}${url}SHA-512`);
expect(a.status).toBe(200);
});
});
describe('digest-int', () => {
const url = `/digest-auth/auth-int/${username}/${passwd}/`;
test('MD5-int (not support)', () => {
expect(axios.get(`${base}${url}MD5`)).rejects.toThrow('error');
});
test('SHA-256-int (not support)', () => {
expect(axios.get(`${base}${url}SHA-256`)).rejects.toThrow('error');
});
test('SHA-512-int (not support)', () => {
expect(axios.get(`${base}${url}SHA-512`)).rejects.toThrow('error');
});
const url = `/digest-auth/auth-int/${username}/${passwd}/`;
test('MD5-int (not support)', () => {
expect(axios.get(`${base}${url}MD5`)).rejects.toThrow('error');
});
test('SHA-256-int (not support)', () => {
expect(axios.get(`${base}${url}SHA-256`)).rejects.toThrow('error');
});
test('SHA-512-int (not support)', () => {
expect(axios.get(`${base}${url}SHA-512`)).rejects.toThrow('error');
});
});

describe('no auth', () => {
const url = '/status/200';
test('GET', async () => {
const a = await axios.get(`${base}${url}`);
expect(a.status).toBe(200);
});
test('POST', async () => {
const a = await axios.post(`${base}${url}`);
expect(a.status).toBe(200);
});
test('PUT', async () => {
const a = await axios.put(`${base}${url}`);
expect(a.status).toBe(200);
});
test('DELETE', async () => {
const a = await axios.delete(`${base}${url}`);
expect(a.status).toBe(200);
});
const url = '/status/200';
test('GET', async () => {
const a = await axios.get(`${base}${url}`);
expect(a.status).toBe(200);
});
test('POST', async () => {
const a = await axios.post(`${base}${url}`);
expect(a.status).toBe(200);
});
test('PUT', async () => {
const a = await axios.put(`${base}${url}`);
expect(a.status).toBe(200);
});
test('DELETE', async () => {
const a = await axios.delete(`${base}${url}`);
expect(a.status).toBe(200);
});
});
Loading