merge from local

This commit is contained in:
SrGooglo 2024-04-15 16:35:36 +00:00
parent 126bad9c1e
commit 4911d7b1af
16 changed files with 365 additions and 249 deletions

View File

@ -1,14 +1,13 @@
import React from "react"
export default (method, ...args) => {
if (typeof method !== "function") {
throw new Error("useRequest: method must be a function")
}
const [loading, setLoading] = React.useState(true)
const [result, setResult] = React.useState(null)
const [error, setError] = React.useState(null)
if (typeof method !== "function") {
return [() => {}, null, new Error("Method is not a function"), () => {}]
}
const makeRequest = (...newArgs) => {
method(...newArgs)
.then((data) => {

View File

@ -48,15 +48,16 @@ export default class AuthModel {
}
static register = async (payload) => {
const { username, password, email } = payload
const { username, password, email, tos } = payload
const response = await request({
method: "post",
url: "/auth/register",
url: "/register",
data: {
username,
password,
email,
accept_tos: tos,
}
}).catch((error) => {
console.error(error)

View File

@ -0,0 +1,11 @@
import request from "../../../request"
export default async () => {
const response = await request({
method: "GET",
url: "/music/playlists/featured",
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,36 @@
async function exportObjs() {
if (window) {
let paths = {
...import.meta.glob("./**.ts"),
...import.meta.glob("./**.js"),
}
let fns = {}
for (const path in paths) {
const name = path.split("/").pop().replace(".ts", "").replace(".js", "")
const fn = await paths[path]()
fns[name] = fn.default
}
return fns
} else {
let objs = {}
const dirs = fs.readdirSync(__dirname).filter(file => file !== "index.js")
const fs = require("fs")
const path = require("path")
dirs.forEach((file) => {
const model = require(path.join(__dirname, file)).default
objs[file.replace(".js", "")] = model
})
return objs
}
}
export default await exportObjs()

View File

@ -0,0 +1,11 @@
import request from "../../../request"
export default async (id: String) => {
const response = await request({
method: "GET",
url: `/music/playlists/${id}/data`,
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,11 @@
import request from "../../../request"
export default async (id: String) => {
const response = await request({
method: "GET",
url: `/music/playlists/${id}/items`,
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,29 @@
import request from "../../../request"
type Arguments = {
keywords: String
user_id: String
limit: Number
offset: Number
}
export default async ({
keywords,
user_id,
limit,
offset,
}: Arguments) => {
const response = await request({
method: "GET",
url: "/music/playlists",
params: {
keywords: keywords,
user_id: user_id,
limit: limit,
offset: offset,
}
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,11 @@
import request from "../../../request"
export default async (id: String) => {
const response = await request({
method: "GET",
url: `/music/releases/${id}/data`,
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,29 @@
import request from "../../../request"
type Arguments = {
keywords: String
user_id: String
limit: Number
offset: Number
}
export default async ({
keywords,
user_id,
limit,
offset,
}: Arguments) => {
const response = await request({
method: "GET",
url: "/music/releases",
params: {
keywords: keywords,
user_id: user_id,
limit: limit,
offset: offset,
}
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,26 @@
import request from "../../../request"
type Arguments = {
keywords: String
limit: Number
offset: Number
}
export default async ({
keywords,
limit,
offset,
}: Arguments) => {
const response = await request({
method: "GET",
url: "/music/search",
params: {
keywords: keywords,
limit: limit,
offset: offset,
}
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,12 @@
import request from "../../../request"
export default async (id: String, options: Object) => {
const response = await request({
method: "GET",
url: `/music/tracks/${id}/data`,
params: options
})
// @ts-ignore
return response.data
}

View File

@ -0,0 +1,29 @@
import request from "../../../request"
type Arguments = {
keywords: String
user_id: String
limit: Number
offset: Number
}
export default async ({
keywords,
user_id,
limit,
offset,
}: Arguments) => {
const response = await request({
method: "GET",
url: "/music/tracks",
params: {
keywords: keywords,
user_id: user_id,
limit: limit,
offset: offset,
}
})
// @ts-ignore
return response.data
}

View File

@ -2,60 +2,87 @@ import request from "../../request"
import pmap from "p-map"
import SyncModel from "../sync"
import Getters from "./getters"
export default class MusicModel {
static get api_instance() {
return globalThis.__comty_shared_state.instances["music"]
}
/**
* Performs a search based on the provided keywords, with optional parameters for limiting the number of results and pagination.
*
* @param {string} keywords - The keywords to search for.
* @param {object} options - An optional object containing additional parameters.
* @param {number} options.limit - The maximum number of results to return. Defaults to 5.
* @param {number} options.offset - The offset to start returning results from. Defaults to 0.
* @param {boolean} options.useTidal - Whether to use Tidal for the search. Defaults to false.
* @return {Promise<Object>} The search results.
*/
public static search = Getters.search
/**
* Retrieves the official featured playlists.
*
* @return {Promise<Object>} The data containing the featured playlists.
*/
static async getFeaturedPlaylists() {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: "/featured/playlists",
})
return response.data
}
* Retrieves playlist items based on the provided parameters.
*
* @param {Object} options - The options object.
* @param {string} options.playlist_id - The ID of the playlist.
* @param {string} options.service - The service from which to retrieve the playlist items.
* @param {number} options.limit - The maximum number of items to retrieve.
* @param {number} options.offset - The number of items to skip before retrieving.
* @return {Promise<Object>} Playlist items data.
*/
public static getPlaylistItems = Getters.PlaylistItems
/**
* Retrieves track data for a given ID.
*
* @param {string} id - The ID of the track.
* @return {Promise<Object>} The track data.
*/
static async getTrackData(id) {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/tracks/${id}/data`,
})
return response.data
}
* Retrieves playlist data based on the provided parameters.
*
* @param {Object} options - The options object.
* @param {string} options.playlist_id - The ID of the playlist.
* @param {string} options.service - The service to use.
* @param {number} options.limit - The maximum number of items to retrieve.
* @param {number} options.offset - The offset for pagination.
* @return {Promise<Object>} Playlist data.
*/
public static getPlaylistData = Getters.PlaylistData
/**
* Retrieves tracks data for the given track IDs.
*
* @param {Array} ids - An array of track IDs.
* @return {Promise<Object>} A promise that resolves to the tracks data.
*/
static async getTracksData(ids) {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/tracks/many`,
params: {
ids,
}
})
* Retrieves releases based on the provided parameters.
* If user_id is not provided, it will retrieve self authenticated user releases.
*
* @param {object} options - The options for retrieving releases.
* @param {string} options.user_id - The ID of the user.
* @param {string[]} options.keywords - The keywords to filter releases by.
* @param {number} options.limit - The maximum number of releases to retrieve.
* @param {number} options.offset - The offset for paginated results.
* @return {Promise<Object>} - A promise that resolves to the retrieved releases.
*/
public static getReleases = Getters.releases
/**
* Retrieves release data by ID.
*
* @param {number} id - The ID of the release.
* @return {Promise<Object>} The release data.
*/
public static getReleaseData = Getters.releaseData
/**
* Retrieves track data for a given ID.
*
* @param {string} id - The ID of the track or multiple IDs separated by commas.
* @return {Promise<Object>} The track data.
*/
public static getTrackData = Getters.trackData
/**
* Retrieves the official featured playlists.
*
* @return {Promise<Object>} The data containing the featured playlists.
*/
public static getFeaturedPlaylists = Getters.featuredPlaylists
//!INCOMPLETE
return response.data
}
/**
* Retrieves favorite tracks based on specified parameters.
@ -78,9 +105,8 @@ export default class MusicModel {
const requesters = [
async () => {
let { data } = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/tracks/liked`,
url: `/music/tracks/liked`,
params: {
limit: limitPerRequesters,
offset,
@ -138,6 +164,8 @@ export default class MusicModel {
}
}
/**
* Retrieves favorite playlists based on the specified parameters.
*
@ -155,16 +183,7 @@ export default class MusicModel {
const requesters = [
async () => {
const { data } = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/playlists/self`,
params: {
keywords,
},
})
return data
return await MusicModel.getMyReleases(keywords)
},
]
@ -217,117 +236,9 @@ export default class MusicModel {
}
}
/**
* Retrieves playlist items based on the provided parameters.
*
* @param {Object} options - The options object.
* @param {string} options.playlist_id - The ID of the playlist.
* @param {string} options.service - The service from which to retrieve the playlist items.
* @param {number} options.limit - The maximum number of items to retrieve.
* @param {number} options.offset - The number of items to skip before retrieving.
* @return {Promise<Object>} Playlist items data.
*/
static async getPlaylistItems({
playlist_id,
service,
limit,
offset,
}) {
if (service === "tidal") {
const result = await SyncModel.tidalCore.getPlaylistItems({
playlist_id,
limit,
offset,
resolve_items: true,
})
return result
}
const { data } = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/playlists/${playlist_id}/items`,
params: {
limit,
offset,
}
})
return data
}
/**
* Retrieves playlist data based on the provided parameters.
*
* @param {Object} options - The options object.
* @param {string} options.playlist_id - The ID of the playlist.
* @param {string} options.service - The service to use.
* @param {number} options.limit - The maximum number of items to retrieve.
* @param {number} options.offset - The offset for pagination.
* @return {Promise<Object>} Playlist data.
*/
static async getPlaylistData({
playlist_id,
service,
limit,
offset,
}) {
if (service === "tidal") {
const result = await SyncModel.tidalCore.getPlaylistData({
playlist_id,
limit,
offset,
resolve_items: true,
})
return result
}
const { data } = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/playlists/${playlist_id}/data`,
params: {
limit,
offset,
}
})
return data
}
/**
* Performs a search based on the provided keywords, with optional parameters for limiting the number of results and pagination.
*
* @param {string} keywords - The keywords to search for.
* @param {object} options - An optional object containing additional parameters.
* @param {number} options.limit - The maximum number of results to return. Defaults to 5.
* @param {number} options.offset - The offset to start returning results from. Defaults to 0.
* @param {boolean} options.useTidal - Whether to use Tidal for the search. Defaults to false.
* @return {Promise<Object>} The search results.
*/
static async search(keywords, { limit = 5, offset = 0, useTidal = false }) {
const { data } = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/search`,
params: {
keywords,
limit,
offset,
useTidal,
},
})
return data
}
/**
* Creates a new playlist.
@ -337,7 +248,6 @@ export default class MusicModel {
*/
static async newPlaylist(payload) {
const { data } = await request({
instance: MusicModel.api_instance,
method: "POST",
url: `/playlists/new`,
data: payload,
@ -355,7 +265,6 @@ export default class MusicModel {
*/
static async putPlaylistItem(playlist_id, item) {
const response = await request({
instance: MusicModel.api_instance,
method: "PUT",
url: `/playlists/${playlist_id}/items`,
data: item,
@ -373,7 +282,6 @@ export default class MusicModel {
*/
static async deletePlaylistItem(playlist_id, item_id) {
const response = await request({
instance: MusicModel.api_instance,
method: "DELETE",
url: `/playlists/${playlist_id}/items/${item_id}`,
})
@ -389,7 +297,6 @@ export default class MusicModel {
*/
static async deletePlaylist(playlist_id) {
const response = await request({
instance: MusicModel.api_instance,
method: "DELETE",
url: `/playlists/${playlist_id}`,
})
@ -405,7 +312,6 @@ export default class MusicModel {
*/
static async putRelease(payload) {
const response = await request({
instance: MusicModel.api_instance,
method: "PUT",
url: `/releases/release`,
data: payload
@ -415,72 +321,6 @@ export default class MusicModel {
}
/**
* Retrieves the releases associated with the authenticated user.
*
* @param {string} keywords - The keywords to filter the releases by.
* @return {Promise<Object>} A promise that resolves to the data of the releases.
*/
static async getMyReleases(keywords) {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/releases/self`,
params: {
keywords,
}
})
return response.data
}
/**
* Retrieves releases based on the provided parameters.
*
* @param {object} options - The options for retrieving releases.
* @param {string} options.user_id - The ID of the user.
* @param {string[]} options.keywords - The keywords to filter releases by.
* @param {number} options.limit - The maximum number of releases to retrieve.
* @param {number} options.offset - The offset for paginated results.
* @return {Promise<Object>} - A promise that resolves to the retrieved releases.
*/
static async getReleases({
user_id,
keywords,
limit = 50,
offset = 0,
}) {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/releases/user/${user_id}`,
params: {
keywords,
limit,
offset,
}
})
return response.data
}
/**
* Retrieves release data by ID.
*
* @param {number} id - The ID of the release.
* @return {Promise<Object>} The release data.
*/
static async getReleaseData(id) {
const response = await request({
instance: MusicModel.api_instance,
method: "GET",
url: `/releases/${id}/data`
})
return response.data
}
/**
* Deletes a release by its ID.
*
@ -489,7 +329,6 @@ export default class MusicModel {
*/
static async deleteRelease(id) {
const response = await request({
instance: MusicModel.api_instance,
method: "DELETE",
url: `/releases/${id}`
})
@ -510,7 +349,6 @@ export default class MusicModel {
}
const response = await request({
instance: MusicModel.api_instance,
method: "POST",
url: `/tracks/${track_id}/refresh-cache`,
})
@ -546,7 +384,6 @@ export default class MusicModel {
}
default: {
const response = await request({
instance: MusicModel.api_instance,
method: to ? "POST" : "DELETE",
url: `/tracks/${track_id}/like`,
params: {
@ -558,4 +395,4 @@ export default class MusicModel {
}
}
}
}
}

View File

@ -118,7 +118,7 @@ export default class User {
static checkUsernameAvailability = async (username) => {
const { data } = await request({
method: "GET",
url: `/user/username_available`,
url: `/availability`,
params: {
username,
}
@ -130,7 +130,7 @@ export default class User {
static checkEmailAvailability = async (email) => {
const { data } = await request({
method: "GET",
url: `/user/email_available`,
url: `/availability`,
params: {
email,
}

View File

@ -0,0 +1,52 @@
import request from "../request"
// create a regex to detect params with %% symbol, from the url
const paramMatchRegex = /(%[0-9a-f]{2}%)/g
export default (method: string = "GET", url: string = "/", params?: object, data?: object) => {
return async function generatedRequest(arg0: any, arg1: any) {
const requestObj = {
method: method,
url: url,
params: params,
data: data,
}
// search url for params
// example: /namespace/search/[0]/data => /namespace/search/${arguments[0]}/data
// if no url matches, merge params with arg0 and override data in requestObj
if (url.match(paramMatchRegex)) {
requestObj.url = url.replace(paramMatchRegex, (match) => {
console.log(match)
// replace with arguments
const fnArgumentIndex = ""
return match
})
} else {
requestObj.params = {
...requestObj.params,
...arg0
}
requestObj.data = {
...requestObj.data,
...arg1
}
}
if (typeof requestObj.params === "object" && requestObj.params) {
Object.keys(requestObj.params).forEach((key) => {
if (requestObj.params && typeof requestObj.params[key] === "string") {
}
})
}
const response = await request(requestObj)
// @ts-ignore
return response.data
}
}

22
src/utils/importFrom.js Normal file
View File

@ -0,0 +1,22 @@
async function importFilesFrom(from) {
let paths = {
// @ts-ignore
...import.meta.glob(`${from}/**.ts`),
// @ts-ignore
...import.meta.glob(`${from}/**.js`),
}
let fns = {}
for (const path in paths) {
// @ts-ignore
const name = path.split("/").pop().replace(".ts", "").replace(".js", "")
const fn = await paths[path]()
fns[name] = fn.default
}
return fns
}
export default importFilesFrom