weverse
Version:
Wrapper for weverse private API
282 lines (281 loc) • 7.53 kB
JavaScript
export const isWeverseLogin = (res) => {
return res.access_token && typeof res.access_token === 'string' &&
res.token_type && res.token_type === 'bearer' &&
typeof res.expires_in === 'number' &&
res.refresh_token && typeof res.refresh_token === 'string' &&
res.weMemberId && typeof res.weMemberId === 'number';
};
const optToNum = (val) => {
if (val === undefined || typeof val === 'undefined')
return val;
if (typeof val !== 'string')
throw new Error();
return Number(val);
};
const optNotif = (val) => {
if (val === undefined || typeof val === 'undefined')
return val;
if (typeof val !== 'string')
throw new Error();
if (isNotif(val))
return val;
throw new Error();
};
const string = (val) => {
if (typeof val !== 'string')
throw new Error();
return val;
};
const mediaType = (val) => {
if (typeof val !== 'string')
throw new Error();
if (val === 'VIDEO' || val === 'PHOTO')
return val;
throw new Error();
};
const boolean = (val) => {
if (typeof val !== 'boolean')
throw new Error();
return val;
};
const number = (val) => {
if (typeof val !== 'number')
throw new Error();
return val;
};
const url = (val) => {
if (val === undefined)
return new URL('https://PLACEHOLDER.weverse.com');
if (typeof val !== 'string')
throw new Error();
const url = new URL(val);
return url;
};
const toNum = (val) => {
if (typeof val !== 'string')
throw new Error();
return Number(val);
};
const date = (val) => {
if (typeof val !== 'string')
throw new Error();
return new Date(val);
};
const optionalUrl = (val) => {
if (typeof val === 'undefined')
return undefined;
if (typeof val === 'string')
return new URL(val);
throw new Error();
};
const optionalDate = (val) => {
if (typeof val === 'string')
return date(val);
return undefined;
};
const array = (inner) => (val) => {
if (!Array.isArray(val))
throw new Error();
return val.map(inner);
};
const notif = (val) => {
if (typeof val !== 'string')
throw new Error();
if (isNotif(val))
return val;
throw new Error();
};
const isNotif = (val) => {
for (const [k, v] of Object.entries(NotificationType)) {
if (v === val)
return true;
}
return false;
};
const optional = (inner) => (val) => {
if (val === undefined)
return undefined;
return inner(val);
};
const object = (inner) => {
return (val) => {
if (val === null || typeof val !== 'object')
throw new Error();
const out = {};
for (const k in inner) {
out[k] = inner[k](val[k]);
}
return out;
};
};
export const Community = object({
id: number,
name: string,
description: string,
memberCount: number,
homeBannerImgPath: url,
iconImgPath: url,
bannerImgPath: url,
fullname: array(string),
fcMember: boolean,
membershipName: optional(string),
});
export const CommunityArray = array(Community);
export const Artist = object({
id: number,
communityUserId: number,
name: string,
listName: array(string),
profileNickName: string,
profileImgPath: url,
profileUploadImgPath: url,
isBirthday: boolean,
groupName: string,
communityId: number,
hasNewToFans: boolean,
hasNewPrivateToFans: boolean,
toFanLastId: optional(number),
toFanLastCreatedAt: optionalDate,
birthdayImgUrl: url,
});
export const ArtistArray = array(Artist);
export var NotificationType;
(function (NotificationType) {
NotificationType["MEDIA"] = "MEDIA";
NotificationType["SERVICE"] = "SERVICE_NOTICE";
NotificationType["FANS"] = "TO_FANS";
NotificationType["POST"] = "ARTIST_POST";
NotificationType["FAN_REPLY"] = "COMMENT_DETAIL";
NotificationType["NOTICE"] = "NOTICE";
NotificationType["O_POST"] = "POST";
})(NotificationType || (NotificationType = {}));
export const ExtraInfo = object({
replyCommentId: toNum,
originContentId: toNum,
originContentType: notif,
});
// const isInfo = (val: unknown): val is Info => {
// try {
// ExtraInfo(val)
// return true
// } catch {
// return false
// }
// }
export const Notification = object({
id: number,
message: string,
boldElement: string,
communityId: number,
communityName: string,
contentsExtraInfo: object({
replyCommentId: optToNum,
originContentId: optToNum,
originContentType: optNotif
}),
contentsType: notif,
contentsId: number,
notifiedAt: date,
iconImageUrl: url,
artistId: optional(number),
isMembershipContent: boolean,
isWebOnly: boolean,
platform: string,
});
export const NotificationArray = array(Notification);
export const isNotification = (n) => {
return !!n;
};
export const Photo = object({
mediaId: optional(number),
postId: number,
imgUrl: url,
imgWidth: number,
imgHeight: number,
id: number,
contentIndex: number,
thumbnailImgUrl: url,
thumbnailImgWidth: number,
thumbnailImgHeight: number,
orgImgUrl: url,
orgImgWidth: number,
orgImgHeight: number,
downloadImgFilename: string,
isGif: boolean
});
export const Video = object({
id: optional(number),
videoUrl: url,
thumbnailUrl: url,
thumbnailWidth: number,
thumbnailHeight: number,
playTime: number
});
export const artistInfo = object({
artistId: number,
communityId: number,
});
export const Comment = object({
id: number,
body: string,
commentCount: number,
likeCount: number,
postId: number,
createdAt: date,
updatedAt: date,
communityUser: artistInfo
});
export const Post = object({
id: number,
communityUser: artistInfo,
communityTabId: number,
body: optional(string),
artistComments: optional(array(Comment)),
commentCount: number,
likeCount: number,
createdAt: date,
updatedAt: date,
photos: optional(array(Photo)),
attachedVideos: optional(array(Video)),
});
export const PostArray = array(Post);
export const PhotoArray = array(Photo);
export const isPost = (n) => {
return !!n;
};
export const isComment = (c) => {
return !!c;
};
export const Media = object({
id: number,
communityId: number,
body: string,
type: mediaType,
thumbnailPath: url,
title: string,
extVideoPath: optionalUrl,
youtubeId: optional(string),
likeCount: number,
playCount: number,
commentCount: number,
createdAt: date,
updatedAt: date,
photos: array(Photo)
});
export const MediaArray = array(Media);
export const CommentArray = array(Comment);
export const NotifContent = {
COMMENT: ["commented on", "replied to", "포스트에 댓글을 작성했습니다", "답글을 작성했습니다."],
POST: [
"님이 포스트를 작성했습니다", "created a new post!", "shared a moment with you", "모먼트가 도착했습니다"
],
MEDIA: ["Check out the new media", "새로운 미디어"],
ANNOUNCEMENT: ["New announcement", "NOTICE:", "(광고)", "(AD)"]
};
export var NotifKeys;
(function (NotifKeys) {
NotifKeys["COMMENT"] = "COMMENT";
NotifKeys["POST"] = "POST";
NotifKeys["MEDIA"] = "MEDIA";
NotifKeys["ANNOUNCEMENT"] = "ANNOUNCEMENT";
})(NotifKeys || (NotifKeys = {}));