Merge pull request #1 from MarciiTheDev/feature/SyntaxUpdate
Feature/syntax update
This commit is contained in:
commit
05f0beedc7
5
src/MarcSync/Errors/Authorization.lua
Normal file
5
src/MarcSync/Errors/Authorization.lua
Normal file
@ -0,0 +1,5 @@
|
||||
return {
|
||||
InvalidAccessToken = function(message: string):string
|
||||
return ("[MarcSync Exception] InvalidAccessToken: %s"):format(message)
|
||||
end
|
||||
}
|
8
src/MarcSync/Errors/Collection.lua
Normal file
8
src/MarcSync/Errors/Collection.lua
Normal file
@ -0,0 +1,8 @@
|
||||
return {
|
||||
CollectionNotFound = function(message: string):string
|
||||
return ("[MarcSync Exception] CollectionNotFound: %s"):format(message)
|
||||
end,
|
||||
CollectionAlreadyExists = function(message: string):string
|
||||
return ("[MarcSync Exception] CollectionAlreadyExists: %s"):format(message)
|
||||
end
|
||||
}
|
8
src/MarcSync/Errors/Entry.lua
Normal file
8
src/MarcSync/Errors/Entry.lua
Normal file
@ -0,0 +1,8 @@
|
||||
return {
|
||||
InvalidEntryData = function(message: string):string
|
||||
return ("[MarcSync Exception] InvalidEntryData: %s"):format(message)
|
||||
end,
|
||||
EntryNotFound = function(message: string):string
|
||||
return ("[MarcSync Exception] EntryNotFound: %s"):format(message)
|
||||
end
|
||||
}
|
@ -1,211 +0,0 @@
|
||||
|
||||
local tokens = {
|
||||
["exampleToken"] = ""
|
||||
}
|
||||
|
||||
|
||||
-- DO NOT EDIT THE FOLLOWING LINES BELOW, UNLESS YOU KNOW WHAT YOU ARE DOING!
|
||||
|
||||
local Utils = require(script.Parent.Utils)
|
||||
|
||||
type Function = () -> ()
|
||||
type DefaultResultType = (success: boolean, errorMessage: string) -> ()
|
||||
type DefaultResult = {
|
||||
["onResult"]: {
|
||||
["Connect"]:DefaultResultType
|
||||
}
|
||||
}
|
||||
|
||||
local HttpService = game:GetService("HttpService")
|
||||
|
||||
local reqQueue = {}
|
||||
|
||||
coroutine.wrap(function()
|
||||
while wait(5) do
|
||||
for i,v in pairs(reqQueue) do
|
||||
local result;
|
||||
local success, Error = pcall(function() result = v.method(v.arguments) end)
|
||||
if not success and Error == "Number of requests exceeded limit" then
|
||||
break
|
||||
else
|
||||
v.event:__Fire(result)
|
||||
table.remove(reqQueue, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
function signal()
|
||||
local RBXSignal = {}
|
||||
RBXSignal._bindableEvent = Instance.new("BindableEvent")
|
||||
function RBXSignal:_Fire(...)
|
||||
RBXSignal._bindableEvent:Fire(...)
|
||||
end
|
||||
function RBXSignal:Connect(handler: ({success: boolean, result: {}}) -> ({success: boolean, result: {}}))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not (type(handler) == "function") then
|
||||
error(("connect(%s)"):format(typeof(handler)), 2)
|
||||
end
|
||||
RBXSignal._bindableEvent.Event:Connect(function(...)
|
||||
handler(...)
|
||||
end)
|
||||
end
|
||||
return RBXSignal
|
||||
end
|
||||
|
||||
local marcsync = {}
|
||||
marcsync.__index = marcsync
|
||||
marcsync.request = {}
|
||||
function marcsync.new(token: string)
|
||||
if not token then warn("Token not provided while creating a new MarcSync Object.") end
|
||||
if not tokens[token] then warn("Token provided for creating a new MarcSync Object not Found in Token Table, using it as token instead.") else token = tokens[token] end
|
||||
local self = setmetatable({}, marcsync)
|
||||
self._token = token
|
||||
self.request._parent = self
|
||||
self.request.version._parent = self.request
|
||||
self.request.database._parent = self.request
|
||||
self.request.collection._parent = self.request
|
||||
self.utils._parent = self
|
||||
return self
|
||||
end
|
||||
|
||||
function marcsync:_checkInstallation()
|
||||
if not self then error("Please Setup MarcSync before using MarcSync.") end
|
||||
if not self._token then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
--print(HttpService.HttpEnabled)
|
||||
--if not HttpService.HttpEnabled then error("Please Enable HTTPService in order to use MarcSync.") end
|
||||
end
|
||||
|
||||
function marcsync._errorHandler(RBXSignal: {}, Error: string)
|
||||
spawn(function()
|
||||
RBXSignal:_Fire({
|
||||
["success"] = false,
|
||||
["errorMessage"] = Error
|
||||
})
|
||||
end)
|
||||
return {["onResult"] = RBXSignal}
|
||||
end
|
||||
|
||||
function marcsync:_requestHandler(result: {}, Error: string):{["onResult"]: {}}
|
||||
if result == nil then return self._errorHandler(signal(), Error) end
|
||||
local errorResult;
|
||||
local signalevent;
|
||||
if #result.Body == 0 and not result.Success then
|
||||
errorResult = "HTTP "..result.StatusCode.." ("..result.StatusMessage..")" result = false
|
||||
elseif not pcall(function() HttpService:JSONDecode(result.Body) end) then
|
||||
error("Unexpected MarcSync Result.")
|
||||
else
|
||||
result = result.Body
|
||||
signalevent = {["onResult"]=signal()}
|
||||
end
|
||||
spawn(function()
|
||||
if not result then return end
|
||||
signalevent.onResult:_Fire(result)
|
||||
end)
|
||||
return signalevent or self._errorHandler(signal(), errorResult)
|
||||
end
|
||||
|
||||
marcsync.request.version = {}
|
||||
function marcsync.request.version:get(clientId: number?):{["success"]:boolean,["version"]:string,["update_server"]:string}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
local url = ""
|
||||
if clientId then url = "/"..clientId end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/utils/version"..url)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
|
||||
marcsync.request.collection = {}
|
||||
function marcsync.request.collection:create(collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("POST", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._parent._parent._token)).onResult:Connect(function(any)
|
||||
if not any["success"] then result = false return end
|
||||
result = require(script.Parent.Objects.Collection)._new(collectionName, self._parent._parent._token)
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
function marcsync.request.collection:fetch(collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._parent._parent._token)).onResult:Connect(function(any)
|
||||
if not any["success"] then result = false return end
|
||||
result = require(script.Parent.Objects.Collection)._new(collectionName, self._parent._parent._token)
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
function marcsync.request.collection:get(collectionName: string):typeof(require(script.Parent.Objects.Collection))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
return require(script.Parent.Objects.Collection)._new(collectionName, self._parent._parent._token)
|
||||
end
|
||||
marcsync.request.database = {}
|
||||
function marcsync.request.database:fetch(databaseId: number):DefaultResult
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
if not databaseId then error("No DatabaseId Provided") end
|
||||
local result;
|
||||
local success, Error = pcall(function() result = HttpService:RequestAsync({Url = "https://api.marcthedev.it/marcsync/v0/database/"..databaseId, Headers = {["Authorization"]=self._parent._parent._token}}) end)
|
||||
return self._parent._parent:_requestHandler(result, Error)
|
||||
end
|
||||
function marcsync.request.database:delete(databaseId: number):DefaultResult
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
self._parent._parent:_checkInstallation()
|
||||
if not databaseId then error("No DatabaseId Provided") end
|
||||
local result;
|
||||
local success, Error = pcall(function() result = HttpService:RequestAsync({Url = "https://api.marcthedev.it/marcsync/v0/database/"..databaseId, Headers = {["Authorization"]=self._parent._parent._token}}) end)
|
||||
return self._parent._parent:_requestHandler(result, Error)
|
||||
end
|
||||
marcsync.utils = {}
|
||||
function marcsync.utils.safeRequest(method: Function, arguments: {})
|
||||
--_checkInstallation()
|
||||
|
||||
|
||||
--[[
|
||||
spawn(function()
|
||||
local result;
|
||||
local success, Error = pcall(function() result = method(arguments) end)
|
||||
if not success and Error == "Number of requests exceeded limit" then
|
||||
reqQueue[#reqQueue] = {["method"] = method, ["arguments"] = arguments, ["event"] = RBXSignal}
|
||||
elseif success then
|
||||
RBXSignal:__Fire(result)
|
||||
end
|
||||
end)
|
||||
|
||||
return { ["onResult"] = RBXSignal}
|
||||
--]]
|
||||
end
|
||||
function marcsync.utils.bulkRequest(methods: {}, safeReq: boolean?):{}
|
||||
--_checkInstallation()
|
||||
local returns = {}
|
||||
for i,method in pairs(methods) do if safeReq then marcsync.safeRequest(method[1], method[2]).onResult:Connect(function(result) returns[i] = result end) else returns[i] = method[1](method[2]) end end
|
||||
while #methods ~= #returns do
|
||||
wait()
|
||||
end
|
||||
return returns
|
||||
end
|
||||
|
||||
return marcsync
|
65
src/MarcSync/MarcSyncv1.0.lua
Normal file
65
src/MarcSync/MarcSyncv1.0.lua
Normal file
@ -0,0 +1,65 @@
|
||||
local tokens = {
|
||||
["exampleToken"] = ""
|
||||
}
|
||||
|
||||
|
||||
-- DO NOT EDIT THE FOLLOWING LINES BELOW, UNLESS YOU KNOW WHAT YOU ARE DOING!
|
||||
|
||||
local Utils = require(script.Parent.Utils)
|
||||
local MarcSyncClient = {}
|
||||
|
||||
MarcSyncClient.getVersion = function(self:typeof(MarcSyncClient), clientId: number?):string
|
||||
self:_checkInstallation()
|
||||
local url = ""
|
||||
if clientId then url = "/"..clientId end
|
||||
local result = Utils.makeHTTPRequest("GET", "https://api.marcsync.dev/v0/utils/version"..url);
|
||||
return result["version"]
|
||||
end
|
||||
|
||||
MarcSyncClient.createCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
if not self._accessToken then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = Utils.makeHTTPRequest("collection", "POST", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
result = require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
|
||||
return result
|
||||
end
|
||||
MarcSyncClient.fetchCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
self:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
local result = Utils.makeHTTPRequest("collection", "GET", "https://api.marcsync.dev/v0/collection/"..collectionName, {}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
result = require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
|
||||
return result
|
||||
end
|
||||
MarcSyncClient.getCollection = function(self:typeof(MarcSyncClient), collectionName: string):typeof(require(script.Parent.Objects.Collection).new())
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
self:_checkInstallation()
|
||||
if not collectionName then error("No CollectionName Provided") end
|
||||
return require(script.Parent.Objects.Collection).new(collectionName, self._accessToken)
|
||||
end
|
||||
|
||||
return {
|
||||
new = function(accessToken: string):typeof(MarcSyncClient)
|
||||
if not accessToken then warn("Token not provided while creating a new MarcSync Object.") end
|
||||
if not tokens[accessToken] then warn("Token provided for creating a new MarcSync Object not Found in Token Table, using it as token instead.") else accessToken = tokens[accessToken] end
|
||||
local self = {}
|
||||
self._accessToken = accessToken
|
||||
self._checkInstallation = function()
|
||||
if not self then error("Please Setup MarcSync before using MarcSync.") end
|
||||
if not self._accessToken then error("[MarcSync] Please set a Token before using MarcSync.") end
|
||||
--print(HttpService.HttpEnabled)
|
||||
--if not HttpService.HttpEnabled then error("Please Enable HTTPService in order to use MarcSync.") end
|
||||
end
|
||||
|
||||
self = setmetatable(self, {
|
||||
__index = MarcSyncClient
|
||||
})
|
||||
|
||||
return self
|
||||
end
|
||||
}
|
@ -1,109 +1,76 @@
|
||||
local Utils = require(script.Parent.Parent.Utils)
|
||||
local Entry = require(script.Parent.Entry)
|
||||
|
||||
local collection = {}
|
||||
collection.__index = collection
|
||||
|
||||
local filterSheme = {
|
||||
["values"]=typeof({ ["key"]=... }),
|
||||
["startsWith"]=typeof({ "key" }),
|
||||
["ignoreCases"]=typeof({ "key" })
|
||||
local types = {
|
||||
EntryData = require(script.Parent.Parent.Types.EntryData).getType()
|
||||
}
|
||||
|
||||
function collection:insert(data:{key:any}):typeof(Entry)?
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local Collection = {}
|
||||
|
||||
Collection.createEntry = function(self:typeof(Collection), data:typeof(types.EntryData)):typeof(Entry.new())
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("POST", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["data"]=data}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["objectId"] then
|
||||
data["_id"] = any["objectId"]
|
||||
result = require(script.Parent.Entry)._new(self._collectionName, data, self._token)
|
||||
return
|
||||
local result = Utils.makeHTTPRequest("entry", "POST", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["data"]=data}, self._accessToken);
|
||||
|
||||
if result["success"] and result["objectId"] then
|
||||
data["_id"] = result["objectId"]
|
||||
result = require(script.Parent.Entry).new(self._collectionName, data, self._accessToken)
|
||||
else
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
Collection.updateEntries = function(self:typeof(Collection), filters:typeof(types.EntryData), data:typeof(types.EntryData)):number
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = Utils.makeHTTPRequest("entry", "PUT", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["filters"]=filters,["data"]=data}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
|
||||
return result["modifiedEntries"]
|
||||
end
|
||||
|
||||
Collection.getEntries = function(self:typeof(Collection), filters:typeof(types.EntryData)):{[number]:typeof(Entry.new())}
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
if not filters then filters = {} end
|
||||
local result = Utils.makeHTTPRequest("entry", "DELETE", "https://api.marcsync.dev/v0/entries/"..self._collectionName.."?isQuery=true", {["filters"]=filters}, self._accessToken);
|
||||
if result["success"] and result["entries"] then
|
||||
local _result = {}
|
||||
for index,entry in pairs(result["entries"]) do
|
||||
_result[index] = require(script.Parent.Entry).new(self._collectionName, entry, self._accessToken)
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
result = _result
|
||||
else
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function collection:update(filters:typeof(filterSheme),data:{key:any}):{message:string?,errorMessage:string?,success:boolean,modifiedEntries:number?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
Collection.deleteEntries = function(self:typeof(Collection), filters:typeof(types.EntryData)):number
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PUT", "https://api.marcsync.dev/v1/entries/"..self._collectionName, {["filters"]=filters.values,["data"]=data}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/entries/"..self._collectionName, {["filters"]=filters}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
|
||||
return result["deletedEntries"]
|
||||
end
|
||||
|
||||
function collection:select(filters:typeof(filterSheme),limit:number):{entries:{a:typeof(Entry)?,b:typeof(Entry)?,c:typeof(Entry)?}?,success:boolean,message:string?,errorMessage:string?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
Collection.drop = function(self:typeof(Collection))
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PATCH", "https://api.marcsync.dev/v1/entries/"..self._collectionName.."?methodOverwrite=GET", {["filters"]=filters.values, ["limit"]=limit}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["entries"] then
|
||||
local _result = {["entries"]={},["success"]=true}
|
||||
for index,entry in pairs(any["entries"]) do
|
||||
_result["entries"][index] = require(script.Parent.Entry)._new(self._collectionName, entry, self._token)
|
||||
end
|
||||
result = _result
|
||||
return
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("collection", "DELETE", "https://api.marcsync.dev/v0/collection/"..self._collectionName, {}, self._accessToken);
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
self = nil
|
||||
end
|
||||
|
||||
function collection:delete(filters:typeof(filterSheme)):{message:string?,errorMessage:string?,success:boolean,deletedEntries:number?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
if not filters.values then error("[MarcSync: Collection] Invalid arguments given for collection:select(). Expected filters.values, got nil") end
|
||||
filters.values["_startsWith"] = filters.startsWith or {}
|
||||
filters.values["_ignoreCases"] = filters.ignoreCases or {}
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v1/entries/"..self._collectionName, {["filters"]=filters.values}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
return {
|
||||
new = function(collectionName: string, accessToken: string):typeof(Collection)
|
||||
local self = {}
|
||||
self._collectionName = collectionName
|
||||
self._accessToken = accessToken
|
||||
|
||||
function collection:drop():{success:boolean,message:string?,errorMessage:string?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not self._collectionName then error("[MarcSync: Collection] Invalid Object created or trying to access an destroied object.") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/collection/"..self._collectionName, {}, self._token)).onResult:Connect(function(any)
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
end
|
||||
self = setmetatable(self, {
|
||||
__index = Collection
|
||||
})
|
||||
|
||||
collection._collectionName = nil
|
||||
|
||||
function collection._new(_collectionName: string, _token: string):typeof(collection)
|
||||
local self = setmetatable({}, collection)
|
||||
self._collectionName = _collectionName
|
||||
self._token = _token
|
||||
return self
|
||||
end
|
||||
|
||||
return collection
|
||||
return self
|
||||
end
|
||||
}
|
@ -1,62 +1,56 @@
|
||||
local Utils = require(script.Parent.Parent.Utils)
|
||||
|
||||
local entry = {}
|
||||
entry.__index = entry
|
||||
local types = {
|
||||
EntryData = require(script.Parent.Parent.Types.EntryData).getType()
|
||||
}
|
||||
|
||||
function entry:getValue(value:string):any
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local Entry = {}
|
||||
|
||||
Entry.getValue = function(self:typeof(Entry), value:string):any
|
||||
if not value then return nil end
|
||||
return self._values[value]
|
||||
return self._entryData[value]
|
||||
end
|
||||
|
||||
function entry:getValues():{}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
return self._values
|
||||
Entry.getValues = function(self:typeof(Entry)):typeof(types.EntryData)
|
||||
return self._entryData
|
||||
end
|
||||
|
||||
function entry:update(newData:{key:any}):{success:boolean,errorMessage:string?,message:string?,modifiedEntries:IntValue?}
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("PUT", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId},["data"]=newData}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["modifiedEntries"] and any["modifiedEntries"] > 0 then
|
||||
for i,v in pairs(newData) do
|
||||
self._values[i] = v
|
||||
end
|
||||
Entry.updateValues = function(self:typeof(Entry), values:typeof(types.EntryData)):number
|
||||
local result = Utils.makeHTTPRequest("entry", "PUT", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId},["data"]=values}, self._accessToken);
|
||||
|
||||
if result["success"] and result["modifiedEntries"] and result["modifiedEntries"] > 0 then
|
||||
for i,v in pairs(values) do
|
||||
self._entryData[i] = v
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
elseif not result["success"] then
|
||||
error(result["errorMessage"])
|
||||
end
|
||||
|
||||
return result["modifiedEntries"]
|
||||
end
|
||||
|
||||
function entry:delete():{success:boolean,errorMessage:string?,message:string?,deletedEntries:IntValue?}
|
||||
Entry.delete = function(self:typeof(Entry))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
local result = nil;
|
||||
Utils.handleResponse(Utils.makeHTTPRequest("DELETE", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId}}, self._token)).onResult:Connect(function(any)
|
||||
if any["success"] and any["deletedEntries"] then
|
||||
if any["deletedEntries"] < 1 then result = false return end
|
||||
spawn(function()
|
||||
self = nil
|
||||
end)
|
||||
end
|
||||
result = any
|
||||
end)
|
||||
repeat
|
||||
wait()
|
||||
until result ~= nil
|
||||
return result
|
||||
local result = Utils.makeHTTPRequest("entry", "DELETE", "https://api.marcsync.dev/v0/entries/"..self._tableId, {["filters"]={["_id"]=self._objectId}}, self._accessToken);
|
||||
|
||||
if not result["success"] then error(result["errorMessage"]) end
|
||||
self = nil
|
||||
|
||||
end
|
||||
|
||||
function entry._new(_tableId:string, _values:{}, _token:string):typeof(entry)
|
||||
if not _tableId or not _values or not _values["_id"] then error("[MarcSync: Entry] Tried creating invalid Entry Object.") end
|
||||
local self = setmetatable({}, entry)
|
||||
self._tableId = _tableId
|
||||
self._values = _values
|
||||
self._objectId = _values["_id"]
|
||||
self._token = _token
|
||||
return self
|
||||
end
|
||||
return {
|
||||
new = function(tableId:string, entryData:typeof(types.EntryData), accessToken:string):typeof(Entry)
|
||||
if not tableId or not entryData or not entryData["_id"] or not accessToken then error("[MarcSync: Entry] Tried creating invalid Entry Object.") end
|
||||
local self = {}
|
||||
self._tableId = tableId
|
||||
self._entryData = entryData
|
||||
self._objectId = entryData["_id"]
|
||||
self._accessToken = accessToken
|
||||
|
||||
return entry
|
||||
self = setmetatable(self, {
|
||||
__index = Entry
|
||||
})
|
||||
|
||||
return self
|
||||
end
|
||||
}
|
9
src/MarcSync/Types/EntryData.lua
Normal file
9
src/MarcSync/Types/EntryData.lua
Normal file
@ -0,0 +1,9 @@
|
||||
type EntryData = {
|
||||
[string]: any
|
||||
}
|
||||
|
||||
return {
|
||||
getType = function(): EntryData
|
||||
return {}
|
||||
end,
|
||||
}
|
@ -1 +0,0 @@
|
||||
return require(script.Parent.Parent.Parent.Objects.Collection)
|
@ -1 +0,0 @@
|
||||
return require(script.Parent.Parent.Parent.Objects.Entry)
|
@ -1 +0,0 @@
|
||||
return require(script.Parent.Parent)
|
@ -1,64 +1,60 @@
|
||||
local AuthorizationError = require(script.Parent.Errors.Authorization)
|
||||
local CollectionError = require(script.Parent.Errors.Collection)
|
||||
local EntryError = require(script.Parent.Errors.Entry)
|
||||
|
||||
local HttpService = game:GetService("HttpService")
|
||||
|
||||
function errorHandler(RBXSignal: {}, result: any)
|
||||
function errorHandler(type: string, resultBody: any, resultObject: {})
|
||||
local Error;
|
||||
if typeof(result) == typeof({}) and result["message"] then
|
||||
Error = result["message"]
|
||||
elseif typeof(result) == typeof("") then
|
||||
Error = result
|
||||
if typeof(resultBody) == typeof({}) and resultBody["message"] then
|
||||
Error = resultBody["message"]
|
||||
elseif typeof(resultBody) == typeof("") then
|
||||
Error = resultBody
|
||||
else
|
||||
Error = "An Unexpected Error occoured."
|
||||
end
|
||||
spawn(function()
|
||||
RBXSignal:_Fire({
|
||||
["success"] = false,
|
||||
["errorMessage"] = Error
|
||||
})
|
||||
end)
|
||||
return {["onResult"] = RBXSignal}
|
||||
|
||||
if type == "collection" then
|
||||
if resultObject["StatusCode"] == 401 then
|
||||
Error = AuthorizationError.InvalidAccessToken("InvalidAccessToken")
|
||||
elseif resultObject["StatusCode"] == 404 then
|
||||
Error = CollectionError.CollectionNotFound("CollectionNotFound")
|
||||
elseif resultObject["StatusCode"] == 400 then
|
||||
Error = CollectionError.CollectionAlreadyExists("CollectionAlreadyExists")
|
||||
end
|
||||
elseif type == "entry" then
|
||||
if resultObject["StatusCode"] == 401 then
|
||||
Error = AuthorizationError.InvalidAccessToken("InvalidAccessToken")
|
||||
elseif resultObject["StatusCode"] == 404 then
|
||||
Error = CollectionError.CollectionNotFound("CollectionNotFound")
|
||||
elseif resultObject["StatusCode"] == 400 then
|
||||
Error = EntryError.InvalidEntryData("InvalidEntryData")
|
||||
end
|
||||
end
|
||||
|
||||
return {["success"] = false, ["errorMessage"] = Error}
|
||||
end
|
||||
|
||||
local utils = {}
|
||||
function utils._signal()
|
||||
local RBXSignal = {}
|
||||
RBXSignal._bindableEvent = Instance.new("BindableEvent")
|
||||
function RBXSignal:_Fire(...)
|
||||
RBXSignal._bindableEvent:Fire(...)
|
||||
end
|
||||
function RBXSignal:Connect(handler: ({success: boolean, result: {}}) -> ({success: boolean, result: {}}))
|
||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
||||
if not (type(handler) == "function") then
|
||||
error(("connect(%s)"):format(typeof(handler)), 2)
|
||||
end
|
||||
RBXSignal._bindableEvent.Event:Connect(function(...)
|
||||
handler(...)
|
||||
end)
|
||||
end
|
||||
return RBXSignal
|
||||
end
|
||||
function utils.handleResponse(result: any, error: boolean, signal: RBXScriptSignal?)
|
||||
if error then return result end
|
||||
spawn(function()
|
||||
signal:_Fire(result)
|
||||
end)
|
||||
return {["onResult"] = signal}
|
||||
end
|
||||
|
||||
function utils.makeHTTPRequest(method: string, url: string, body: {}, authorization: string):{["success"]: boolean, ["message"]: string}
|
||||
local result;
|
||||
function utils.makeHTTPRequest(type: string, method: string, url: string, body: {}, authorization: string):{["success"]: boolean, ["message"]: string}
|
||||
local resultObj;
|
||||
local resultBody;
|
||||
local success = pcall(function()
|
||||
if body then body = HttpService:JSONEncode(body) end
|
||||
if (method == "GET" or method == "HEAD") then
|
||||
result = HttpService:JSONDecode(HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}})["Body"])
|
||||
resultObj = HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}})
|
||||
resultBody = HttpService:JSONDecode(resultObj["Body"])
|
||||
else
|
||||
result = HttpService:JSONDecode(HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}, Body=body})["Body"])
|
||||
resultObj = HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}, Body=body})
|
||||
resultBody = HttpService:JSONDecode(resultObj["Body"])
|
||||
end
|
||||
end)
|
||||
if success and result and result["success"] then
|
||||
if result["warning"] then warn('[MarcSync HTTPRequest Handler] MarcSync HTTP Request returned warning for URL "'..url..'" with body: "'..HttpService:JSONEncode(body)..'": '..result["warning"]) end
|
||||
return result, false, utils._signal()
|
||||
if success and resultBody and resultBody["success"] then
|
||||
if resultBody["warning"] then warn('[MarcSync HTTPRequest Handler] MarcSync HTTP Request returned warning for URL "'..url..'" with body: "'..HttpService:JSONEncode(body)..'": '..resultBody["warning"]) end
|
||||
return resultBody
|
||||
end
|
||||
return errorHandler(utils._signal(), result), true
|
||||
return errorHandler(type, resultBody, resultObj)
|
||||
end
|
||||
|
||||
return utils
|
Loading…
x
Reference in New Issue
Block a user