'TypeScript/Express: Method still not being exported
I have one method in my authentication-service.ts file that is not successfully exporting for some reason. This is what the method looks like:
interface AuthResult {
httpCode: number;
message: string;
}
function verifyAuth(req: Request): Promise<AuthResult> {
return new Promise(async (resolve, reject) => {
let verifyResult: AuthResult = {
httpCode: 200,
message: 'OK'
};
const authHeader: string | undefined = req.headers["authorization"];
if (!authHeader) {
verifyResult.httpCode = 400;
verifyResult.message = 'missing Authorization in headers';
reject(verifyResult);
}
let service: string | undefined = undefined;
if (Object.keys(req.headers).includes("looney-service") && !Array.isArray(req.headers["looney-service"])) {
service = req.headers["looney-service"];
}
if (!service) {
verifyResult.httpCode = 400;
verifyResult.message = 'missing Looney-Service in headers';
reject(verifyResult);
}
if (authHeader && service) {
const token: string | null = getTokenFromAuth(authHeader);
if (token) {
const result: string = await verifyService(service, token).catch(err => {
console.log('verifyAuth caught error!', err);
return err;
});
if (result == 'OK') {
verifyResult.httpCode = 200;
verifyResult.message = result;
resolve(verifyResult);
} else {
verifyResult.httpCode = 403;
verifyResult.message = result;
reject(verifyResult);
}
} else {
verifyResult.httpCode = 403;
verifyResult.message = 'missing token';
reject(verifyResult);
}
}
});
}
export { getServiceConfig, getServicePublicKey, verifyService, genJwt, getTokenFromAuth, AuthResult, verifyAuth }
I am importing the file that this method sits in like so:
import * as auth from './services/authentication-service'
However, when I console log auth, the object does not contain the verifyAuth method:
{
getServiceConfig: [Function: getServiceConfig]
getServicePublicKey: [Function: getServicePublicKey]
verifyService: [Function: verifyService]
genJwt: [Function: genJwt]
getTokenFromAuth: [Function: getTokenFromAuth]
}
Why is that? TypeScript is not complaining about any type reference, all that has seemingly been corrected.
If I try to write it as a class of export class Auth {} and just drop all those methods in there, of course remove the function keyword and add the reference to this. I still get an issue with verifyAuth() method, in that case it would say Property 'verifyAuth' does not exist on type typeof Auth', what is the deal with this verifyAuth` method?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
