Reputation: 1999
I am trying to perform an Axios request using the URL type. But I get the error Argument type URL is not assignable to parameter type string
import { Injectable } from '@nestjs/common'
import { NotificationsInterface } from '../interface/notifications.interface'
import { OS, UserType } from '../../global.constants'
const axios = require('axios').default
@Injectable()
export class NotificationsGateway implements NotificationsInterface {
readonly urlDevices = new URL(
'https://blablablabla.amazonaws.com/Prod/devices'
)
async saveDeviceAuthenticationInfos(
userId: number,
userType: UserType,
deviceId: number,
pushToken: string,
operatingSystem: OS
): Promise<void> {
const body = {
userId,
userType,
deviceId,
pushToken,
operatingSystem,
}
await axios.post(this.urlDevices, body)
}
}
How am I suppose to make it work? I am half a mind to put it back into a string, but I feel like it's a regression.
Upvotes: 5
Views: 17075
Reputation: 587
Like the error says, you're giving a URL
where a string
is expected.
If you want to keep using the type URL
for some reasons, you can do the following: give this.urlDevices.toString()
as the first parameter of the axios.post()
method.
Or you can just type your urlDevices
as a string
if you're not planning on using URL
's static methods.
Upvotes: 4