Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1457 from pan93412/refactor/dynamic-to-main
Browse files Browse the repository at this point in the history
refactor: allow using server statically
  • Loading branch information
Binaryify authored Jan 28, 2022
2 parents 91a3ba7 + b02d4d5 commit d665429
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
34 changes: 34 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs')
const path = require('path')
const { cookieToJson } = require('./util')
const request = require('./util/request')

/** @type {Record<string, any>} */
let obj = {}
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach((file) => {
if (!file.endsWith('.js')) return
let fileModule = require(path.join(__dirname, 'module', file))
let fn = file.split('.').shift() || ''
obj[fn] = function (data) {
if (typeof data.cookie === 'string') {
data.cookie = cookieToJson(data.cookie)
}
return fileModule(
{
...data,
cookie: data.cookie ? data.cookie : {},
},
request,
)
}
})

/**
* @type {Record<string, any> & import("./server")}
*/
module.exports = {
...require('./server'),
...obj,
}
18 changes: 18 additions & 0 deletions main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const assert = require('assert')
const main = require('./main')

describe('methods in server.js', () => {
it('has serveNcmApi', () => {
assert.strictEqual(typeof main.serveNcmApi, 'function')
})

it('has getModulesDefinitions', () => {
assert.strictEqual(typeof main.getModulesDefinitions, 'function')
})
})

describe('methods in module', () => {
it('has activate_init_profile', () => {
assert.strictEqual(typeof main.activate_init_profile, 'function')
})
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "网易云音乐 NodeJS 版 API",
"scripts": {
"start": "node app.js",
"test": "mocha -r intelli-espower-loader -t 20000 server.test.js --exit",
"test": "mocha -r intelli-espower-loader -t 20000 server.test.js main.test.js --exit",
"lint": "eslint \"**/*.{js,ts}\"",
"lint-fix": "eslint --fix \"**/*.{js,ts}\"",
"prepare": "husky install",
Expand All @@ -26,7 +26,7 @@
"音乐",
"网易云音乐nodejs"
],
"main": "server.js",
"main": "main.js",
"types": "./interface.d.ts",
"engines": {
"node": ">=12"
Expand Down Expand Up @@ -78,4 +78,4 @@
"prettier": "2.5.1",
"typescript": "4.5.2"
}
}
}
21 changes: 0 additions & 21 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,28 +294,7 @@ async function serveNcmApi(options) {
return appExt
}

let obj = {}
fs.readdirSync(path.join(__dirname, 'module'))
.reverse()
.forEach((file) => {
if (!file.endsWith('.js')) return
let fileModule = require(path.join(__dirname, 'module', file))
obj[file.split('.').shift()] = function (data) {
if (typeof data.cookie === 'string') {
data.cookie = cookieToJson(data.cookie)
}
return fileModule(
{
...data,
cookie: data.cookie ? data.cookie : {},
},
request,
)
}
})

module.exports = {
serveNcmApi,
getModulesDefinitions,
...obj,
}

2 comments on commit d665429

@vercel
Copy link

@vercel vercel bot commented on d665429 Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on d665429 Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

netease-cloud-music-api – ./docs

netease-cloud-music-api-git-master-binaryify.vercel.app
neteasecloudmusicapi.vercel.app

Please sign in to comment.