Skip to content

Add fetch support with axios #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ npm install smsapi --save
```ts
import { SMSAPI } from 'smsapi';

const smsapi = new SMSAPI('oAuthToken');
const USE_FETCH = false;
const smsapi = new SMSAPI('oAuthToken', USE_FETCH);

try {
const result = await smsapi.sms.sendSms('+48605xxxxxx', 'My first message!');
Expand Down
168 changes: 126 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"prepublishOnly": "npm run lint && npm run test"
},
"dependencies": {
"axios": "^1.6.1",
"form-data": "^4.0.0"
"axios": "^1.8.4",
"form-data": "^4.0.2"
},
"devDependencies": {
"@babel/preset-env": "^7.23.3",
Expand Down
8 changes: 4 additions & 4 deletions src/smsapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export class SMSAPI {
public templates: Templates;
public vms: Vms;

constructor(accessToken: string) {
constructor(accessToken: string, useFetch = false) {
this.accessToken = accessToken;

this.httpClient = this.setHttpClient();
this.httpClient = this.setHttpClient(useFetch);

this.contacts = new Contacts(this.httpClient);
this.hlr = new Hlr(this.httpClient);
Expand All @@ -52,9 +52,9 @@ export class SMSAPI {
return `smsapi/js-client:${version}`;
}

private setHttpClient(): AxiosInstance {
private setHttpClient(useFetch = false): AxiosInstance {
const httpClient = axios.create({
adapter: 'http',
adapter: useFetch ? 'fetch' : 'http',
baseURL: API_URL,
headers: {
Accept: 'application/json',
Expand Down