Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

refactor: migrate to es modules #136

Merged
merged 1 commit into from
Mar 17, 2021
Merged
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
4 changes: 2 additions & 2 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { server } = require('./src/__tests__/mocks/server.js');
const mockfs = require('mock-fs');
import { server } from './src/__tests__/mocks/server.js';
import mockfs from 'mock-fs';

// Establish API mocking before all tests.
beforeAll(() => server.listen());
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/createAlbum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

beforeAll(() => imgur.setClientId('abc123'));

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/deleteImage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('deleteImage()', () => {
describe('delete image response', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/favoriteImage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('favoriteImage()', () => {
describe('favorite image response', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getAPIUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('getAPIUrl()', () => {
test('should return the default API URL, if nothing is set', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/getAuthorizationHeader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

afterEach(imgur.clearAllCredentials);

test('returns provided access code in bearer header', async () => {
const accessToken = 'abc123';
const imgur = require('../imgur.js');
imgur.setAccessToken(accessToken);
const authorizationHeader = await imgur._getAuthorizationHeader();
expect(authorizationHeader).toBe(`Bearer ${accessToken}`);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getClientId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('getClientId()', () => {
test('should return the default client id, if nothing is set', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getGalleryInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

beforeAll(() => imgur.setClientId('abc123'));

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/getMashapeKey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('getMashapeKey()', () => {
test('should return the same client that was set', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/imgurRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

beforeAll(() => imgur.setClientId('abc123'));

Expand Down
6 changes: 1 addition & 5 deletions src/__tests__/mocks/handlers/album.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ const SuccessResponse = {
status: 200,
};

function postHandler(req, res, ctx) {
export function postHandler(req, res, ctx) {
if (!req.headers.has('authorization')) {
return res(ctx.status(401), ctx.json(AuthenticationRequiredResponse));
}

return res(ctx.json(SuccessResponse));
}

module.exports = {
postHandler,
};
9 changes: 2 additions & 7 deletions src/__tests__/mocks/handlers/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function createRedirectUrl(username) {
return `https://somedomain.com#access_token=123accesstoken456&expires_in=315360000&token_type=bearer&refresh_token=123refrestoken456&account_username=${username}&account_id=123456`;
}

function postHandler(req, res, ctx) {
export function postHandler(req, res, ctx) {
const clientId = req.url.searchParams.get('client_id');
const responseType = req.url.searchParams.get('response_type');

Expand All @@ -47,7 +47,7 @@ function postHandler(req, res, ctx) {
);
}

function getHandler(req, res, ctx) {
export function getHandler(req, res, ctx) {
const clientId = req.url.searchParams.get('client_id');
const responseType = req.url.searchParams.get('response_type');

Expand Down Expand Up @@ -76,8 +76,3 @@ function getHandler(req, res, ctx) {
ctx.body(html)
);
}

module.exports = {
postHandler,
getHandler,
};
6 changes: 1 addition & 5 deletions src/__tests__/mocks/handlers/credits.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ const SuccessResponse = {
status: 200,
};

function getHandler(req, res, ctx) {
export function getHandler(req, res, ctx) {
if (!req.headers.has('authorization')) {
return res(ctx.status(401), ctx.json(AuthenticationRequiredResponse));
}

return res(ctx.json(SuccessResponse));
}

module.exports = {
getHandler,
};
6 changes: 1 addition & 5 deletions src/__tests__/mocks/handlers/gallery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function getHandler(req, res, ctx) {
export function getHandler(req, res, ctx) {
const { id } = req.params;
const response = {
data: {
Expand All @@ -11,7 +11,3 @@ function getHandler(req, res, ctx) {
};
return res(ctx.json(response));
}

module.exports = {
getHandler,
};
12 changes: 3 additions & 9 deletions src/__tests__/mocks/handlers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ const FavoriteSuccessResponse = {
status: 200,
};

function postHandler(_req, res, ctx) {
export function postHandler(_req, res, ctx) {
return res(ctx.json(SuccessResponse));
}

function deleteHandler(req, res, ctx) {
export function deleteHandler(req, res, ctx) {
return res(ctx.json(SuccessResponse));
}

function postFavoriteHandler(req, res, ctx) {
export function postFavoriteHandler(req, res, ctx) {
return res(ctx.json(FavoriteSuccessResponse));
}

module.exports = {
postHandler,
postFavoriteHandler,
deleteHandler,
};
20 changes: 8 additions & 12 deletions src/__tests__/mocks/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { rest } = require('msw');
const upload = require('./upload');
const authorize = require('./authorize');
const image = require('./image');
const gallery = require('./gallery');
const credits = require('./credits');
const album = require('./album');
import { rest } from 'msw';
import * as upload from './upload';
import * as authorize from './authorize';
import * as image from './image';
import * as gallery from './gallery';
import * as credits from './credits';
import * as album from './album';

const handlers = [
export const handlers = [
//upload
rest.post('https://api.imgur.com/3/upload', upload.postHandler),

Expand All @@ -31,7 +31,3 @@ const handlers = [
// album
rest.post('https://api.imgur.com/3/album', album.postHandler),
];

module.exports = {
handlers,
};
6 changes: 1 addition & 5 deletions src/__tests__/mocks/handlers/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const SuccessfulUploadResponse = {
status: 200,
};

function postHandler(req, res, ctx) {
export function postHandler(req, res, ctx) {
// image field is always required
if (!('image' in req.body)) {
return res(ctx.status(400), ctx.json(BadRequestErrorResponse));
Expand All @@ -39,7 +39,3 @@ function postHandler(req, res, ctx) {

return res(ctx.json(SuccessfulUploadResponse));
}

module.exports = {
postHandler,
};
10 changes: 3 additions & 7 deletions src/__tests__/mocks/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const { setupServer } = require('msw/node');
const { handlers } = require('./handlers');
import { setupServer } from 'msw/node';
import { handlers } from './handlers';

const server = setupServer(...handlers);

module.exports = {
server,
};
export const server = setupServer(...handlers);
2 changes: 1 addition & 1 deletion src/__tests__/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('SEARCH', () => {
describe('search options validations', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/setAPIUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('setAPIUrl()', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/setClientId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('setClientId()', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/setMashapeKey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('setMashapeKey()', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/updateInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('updateInfo', () => {
describe('update image metadata', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/uploadFile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

beforeAll(() => imgur.setClientId('abc123'));

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/uploadUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imgur = require('../imgur.js');
import imgur from '../imgur.js';

describe('uploadUrl()', () => {
describe('validation', () => {
Expand Down
16 changes: 9 additions & 7 deletions src/imgur.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
const imgur = exports;
const got = require('got');
const util = require('util');
const fs = require('fs');
import got from 'got';
import util from 'util';
import fs from 'fs';
import FormData from 'form-data';
import { version as VERSION } from '../package.json';
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const FormData = require('form-data');
const VERSION = require('../package.json').version;

const imgur = {};

// The following client ID is tied to the
// registered 'node-imgur' app and is available
Expand Down Expand Up @@ -663,3 +663,5 @@ imgur.clearAllCredentials = () => {
imgurPassword = null;
imgurClientId = defaultClientId;
};

export default imgur;