|
| 1 | +import type { |
| 2 | + LogById, |
| 3 | + LogSearchRequest, |
| 4 | + LogSearchResponse, |
| 5 | + LogUploadRequest, |
| 6 | + LogUploadResponse, |
| 7 | +} from "../types/mod.ts"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Gets the URL of the raw log file |
| 11 | + * @param id The id of the log |
| 12 | + * @returns The url of the log's zip file |
| 13 | + * |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +const getRawLogUrl = (id: string) => `http://logs.tf/logs/log_${id}.log.zip`; |
| 17 | + |
| 18 | +/** |
| 19 | + * The base URL of the for logs.tf |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +const logsBaseUrl = "https://logs.tf"; |
| 23 | + |
| 24 | +/** |
| 25 | + * The logs API url |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +const logsApiUrl = `${logsBaseUrl}/api/v1`; |
| 29 | + |
| 30 | +/** |
| 31 | + * The logs upload url |
| 32 | + * @internal |
| 33 | + */ |
| 34 | +const logsUploadUrl = `${logsBaseUrl}/upload`; |
| 35 | + |
| 36 | +/** |
| 37 | + * Gets a log by id |
| 38 | + * @param logId The log id to get |
| 39 | + * @returns {Promise<LogById>} The log json |
| 40 | + */ |
| 41 | +export async function getById(logId: string): Promise<LogById> { |
| 42 | + if (!logId) { |
| 43 | + throw new Error("LogId cannot be empty!"); |
| 44 | + } |
| 45 | + |
| 46 | + const data = await fetch(`${logsApiUrl}/log/${logId}`); |
| 47 | + |
| 48 | + return (await data.json()) as LogById; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Searches for logs that match the filter |
| 53 | + * @param searchRequest The search request |
| 54 | + * @returns {Promise<LogSearchResponse>} The response of the search |
| 55 | + */ |
| 56 | +export async function search( |
| 57 | + searchRequest: LogSearchRequest |
| 58 | +): Promise<LogSearchResponse> { |
| 59 | + const { |
| 60 | + limit = 1000, |
| 61 | + map = null, |
| 62 | + offset = 0, |
| 63 | + player = [], |
| 64 | + title = null, |
| 65 | + uploader = null, |
| 66 | + } = searchRequest; |
| 67 | + |
| 68 | + if (limit > 10_000) { |
| 69 | + throw new Error("Cannot take more than 10,000 logs at a time"); |
| 70 | + } |
| 71 | + |
| 72 | + const params = new URLSearchParams([ |
| 73 | + ["limit", limit.toString()], |
| 74 | + ["offset", offset.toString()], |
| 75 | + ]); |
| 76 | + |
| 77 | + if (map) { |
| 78 | + params.append("map", map); |
| 79 | + } |
| 80 | + |
| 81 | + if (player) { |
| 82 | + params.append("player", player.join(",")); |
| 83 | + } |
| 84 | + |
| 85 | + if (title) { |
| 86 | + params.append("title", title); |
| 87 | + } |
| 88 | + |
| 89 | + if (uploader) { |
| 90 | + params.append("uploader", uploader); |
| 91 | + } |
| 92 | + |
| 93 | + const response = await fetch(`${logsApiUrl}/log${params.toString()}`); |
| 94 | + |
| 95 | + return (await response.json()) as LogSearchResponse; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Uploads a log to logs.tf |
| 100 | + * @param apiKey The API key |
| 101 | + * @param file The log data to upload |
| 102 | + * @param options The additional information to upload with the log |
| 103 | + * @returns {Promise<LogUploadResponse>} The log upload response |
| 104 | + */ |
| 105 | +export async function uploadLog( |
| 106 | + apiKey: string, |
| 107 | + file: Blob, |
| 108 | + options: LogUploadRequest |
| 109 | +): Promise<LogUploadResponse> { |
| 110 | + const { title, map, updatelog = null, uploader = "node-logs-sdk" } = options; |
| 111 | + |
| 112 | + if (!apiKey) { |
| 113 | + throw new Error("Expected a valid API key, got a nullish value instead"); |
| 114 | + } |
| 115 | + |
| 116 | + if (title) { |
| 117 | + throw new Error("Title cannot be empty!"); |
| 118 | + } |
| 119 | + |
| 120 | + const body = new FormData(); |
| 121 | + const headers = new Headers([["Content-Type", "multipart/form-data"]]); |
| 122 | + |
| 123 | + body.append("logfile", file); |
| 124 | + body.append("title", title); |
| 125 | + body.append("key", apiKey); |
| 126 | + body.append("uploader", uploader!); |
| 127 | + |
| 128 | + if (updatelog) { |
| 129 | + body.append("updatelog", updatelog); |
| 130 | + } |
| 131 | + |
| 132 | + if (map) { |
| 133 | + body.append("map", map); |
| 134 | + } |
| 135 | + |
| 136 | + const response = await fetch(logsUploadUrl, { |
| 137 | + body, |
| 138 | + headers, |
| 139 | + }); |
| 140 | + |
| 141 | + return (await response.json()) as LogUploadResponse; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Gets the raw log file for a given log id |
| 146 | + * @param logId The log id |
| 147 | + * @returns {Promise<Blob>} The raw log file |
| 148 | + */ |
| 149 | +export async function getRawLog(logId: string): Promise<Blob> { |
| 150 | + if (!logId) { |
| 151 | + throw new Error("LogId cannot be empty!"); |
| 152 | + } |
| 153 | + |
| 154 | + const response = await fetch(getRawLogUrl(logId)); |
| 155 | + |
| 156 | + return await response.blob(); |
| 157 | +} |
0 commit comments