Skip to content

Commit

Permalink
cf worker support and luau execution api rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Campbell authored and Cameron Campbell committed Oct 1, 2024
1 parent fa89499 commit 579f11e
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 153 deletions.
283 changes: 260 additions & 23 deletions docs/docs.json

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"name": "openblox",
"description": "Roblox API Wrapper For Both Classic And OpenCloud APIs.",
"type": "commonjs",
"version": "1.0.53",
"license": "MIT",
"bugs": {
"url": "https://github.com/MightyPart/openblox/issues"
},
"repository": {
"type": "github",
"url": "https://github.com/MightyPart/openblox",
"directory": "main"
},
"keywords": [
"Roblox",
"RobloxApiWrapper",
"Opencloud",
"Bedev",
"Bedev2"
],
"exports_DEV": {
".": "./src/index.ts",
"./config": "./src/config/index.ts",
"./classic": "./src/apis/classic/index.ts",
"./classic/*": "./src/apis/classic/*/index.ts",
"./cloud": "./src/apis/cloud/index.ts",
"./cloud/*": "./src/apis/cloud/*/index.ts",
"./cache/adapters": "./src/cache/cacheAdapters/index.ts",
"./http": "./src/http/http.utils.ts",
"./types": "./src/types.ts",
"./queries/cloud": "./src/queries/cloud/index.ts",
"./queries/classic": "./src/queries/classic/index.ts",
"./queries/classic/types": "./src/queries/classic/index.types.ts",
"./helpers": "./src/helpers/index.ts"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./config": {
"types": "./dist/config/index.d.ts",
"default": "./dist/config/index.js"
},
"./classic": {
"types": "./dist/apis/classic/index.d.ts",
"default": "./dist/apis/classic/index.js"
},
"./classic/*": {
"types": "./dist/apis/classic/*/index.d.ts",
"default": "./dist/apis/classic/*/index.js"
},
"./cloud": {
"types": "./dist/apis/cloud/index.d.ts",
"default": "./dist/apis/cloud/index.js"
},
"./cloud/*": {
"types": "./dist/apis/cloud/*/index.d.ts",
"default": "./dist/apis/cloud/*/index.js"
},
"./cache/adapters": {
"types": "./dist/cache/cacheAdapters/index.d.ts",
"default": "./dist/cache/cacheAdapters/index.js"
},
"./http": {
"types": "./dist/http/http.utils.d.ts",
"default": "./dist/http/http.utils.js"
},
"./types": {
"types": "./dist/types.d.ts",
"default": "./dist/types.ts"
},
"./queries/cloud": {
"types": "./dist/queries/cloud/index.d.ts",
"default": "./dist/queries/cloud/index.js"
},
"./queries/classic": {
"types": "./dist/queries/classic/index.d.ts",
"default": "./dist/queries/classic/index.js"
},
"./queries/classic/types": {
"types": "./dist/queries/classic/index.types.d.ts",
"default": "./dist/queries/classic/index.types.js"
},
"./helpers": {
"types": "./dist/helpers/index.d.ts",
"default": "./dist/helpers/index.js"
}
},
"dependencies": {
"lodash": "^4.17.21",
"parse-roblox-errors": "^1.1.10"
},
"devDependencies": {
"@types/lodash": "^4.17.0",
"@types/node": "^22.1.0",
"esbuild": "^0.21.5",
"prettier": "^3.2.5",
"tablemark": "^3.1.0",
"ts-arithmetic": "^0.1.1",
"ts-morph": "^22.0.0",
"delete-cli": "^0.1.3"
},
"peerDependencies": {
"typescript": "^5.0.0",
"typeforge": "^0.0.23"
},
"scripts": {
"build:code": "delete dist && bun run ./build.ts && tsc --emitDeclarationOnly",
"build:docs": "bun run ./docs/buildDocs.ts",
"build": "bun run build:docs && bun run build:code"
}
}
90 changes: 33 additions & 57 deletions src/apis/apiGroup/apiGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ const defaultGetCursors = (rawData: Record<any, any>) => {
}

function isObjectEmpty(obj: Record<any, any>) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
for (var prop in obj) if (obj.hasOwnProperty(prop)) return false
return true
}

const isNoMoreData = (data: any) => {
Expand Down Expand Up @@ -91,12 +88,6 @@ const paginate = (
}
)

const sleep = async (s: number) => new Promise(resolve => setTimeout(resolve, s * 1000))

const expBackoff = (delay: number, lastIter: number) => {
return delay + (5 * lastIter)
}

const pollForResponse = async (url: string, operationPath: string, cloudKey: string) => {
const operationPrefix = operationPath.match(/^(\/?)cloud\/v[1-9]+(\/?)/)
? operationPrefixRegexWithoutVersion.exec(url)?.[1] as UrlSecure
Expand All @@ -107,9 +98,9 @@ const pollForResponse = async (url: string, operationPath: string, cloudKey: str
const headers = { "x-api-key": cloudKey }

let response: any
await pollHttp<{ done?: boolean }>({ method: "GET", url: operationUrl, headers }, async (response, stopPolling) => {
if (!response.body.done) return
response = response
await pollHttp<{ done?: boolean }>({ method: "GET", url: operationUrl, headers }, async (polledResponse, stopPolling) => {
if (!polledResponse.body.done) return
response = polledResponse
stopPolling()
})

Expand Down Expand Up @@ -153,54 +144,39 @@ export const createApiGroup: CreateApiGroupFn = ({ name:groupName, baseUrl, defa
if (oauthToken) headers["Authorization"] = `Bearer ${oauthToken}`
}

let response: HttpResponse = await HttpHandler({ method, url, body, formData, headers }) as any // TODO

if (!(response instanceof HttpResponse)) throw response
let rawData = response.body

// Uncompleted long running operation.
let opPath = rawData?.path
if (opPath && rawData?.done === false && isOpenCloudUrl(url)) {
console.warn(`Polling '${groupName}.${name}' (Please be patient)...`)
response = await pollForResponse(url, pathToPoll ? pathToPoll(rawData) : opPath, cloudKey)

//if (pathToPoll) opPath = pathToPoll(rawData)

/*const operationPrefix = opPath.match(/^(\/?)cloud\/v[1-9]+(\/?)/)
? operationPrefixRegexWithoutVersion.exec(url)?.[1] as UrlSecure
: operationPrefixRegexWithVersion.exec(url)?.[1] as UrlSecure
const opUrl = `${operationPrefix}${opPath}` as UrlSecure
const headers = { "x-api-key": cloudKey }*/

/*let delay = 0
for (let iter = 0; iter > -1; iter++) {
response = await HttpHandler({ method: "GET", url: opUrl, headers })
if (!(response instanceof HttpResponse)) throw response
let main: () => Promise<any>
main = async () => {
let response: HttpResponse = await HttpHandler({ method, url, body, formData, headers }) as any // TODO
if (!(response instanceof HttpResponse)) throw response
let rawData = response.body

// Uncompleted long running operation.
let opPath = rawData?.path
if (opPath && rawData?.done === false && isOpenCloudUrl(url)) {
console.warn(`Polling '${groupName}.${name}' (Please be patient)...`)
response = await pollForResponse(url, pathToPoll ? pathToPoll(rawData) : opPath, cloudKey)
rawData = response.body
}

if (rawData?.done === true) break
await sleep(delay)
delay = expBackoff(delay, iter)
}*/
}

let apiMethodResult: ApiMethodResponse<any, any>
if (formatRawDataFn) apiMethodResult = { response, get data() { return formatRawDataFn(rawData, response) } }
else apiMethodResult = { response, data: rawData }

if (handlerFnCursorArg) {
let [ previousCursor, nextCursor ] = (getCursorsFn ?? thisDefaultGetCursors)(rawData);
apiMethodResult.cursors = { previous: previousCursor, next: nextCursor }
if (args && !("__notRoot" in args)) {
(apiMethodResult as any as ApiMethodResponse<any, any, true>)[Symbol.asyncIterator] = paginate(
apiMethodResult, callApiMethod as CallApiMethod<any, any, true>, args as Record<any, any>, overrides, handlerFnCursorArg
) as any
let apiMethodResult: ApiMethodResponse<any, any> = formatRawDataFn
? { response, again: main, get data() { return formatRawDataFn(rawData, response) } }
: { response, again: main, data: rawData }

// Applies async iterator if method is paginated.
if (handlerFnCursorArg) {
let [ previousCursor, nextCursor ] = (getCursorsFn ?? thisDefaultGetCursors)(rawData);
apiMethodResult.cursors = { previous: previousCursor, next: nextCursor }
if (args && !("__notRoot" in args)) {
(apiMethodResult as any as ApiMethodResponse<any, any, true>)[Symbol.asyncIterator] = paginate(
apiMethodResult, callApiMethod as CallApiMethod<any, any, true>, args as Record<any, any>, overrides, handlerFnCursorArg
) as any
}
}

return apiMethodResult as any
}

return apiMethodResult as any
return await main()
}
}
)
8 changes: 5 additions & 3 deletions src/apis/apiGroup/apiGroup.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ type AddApiMethodHandlerFn<Args extends Record<any, any>, MethodData extends Api
export type ApiMethodResponse<
RawData = any, PrettifiedData = any, ArgsContainsCursor extends boolean | null = null, IsRoot extends boolean = true
> = ArgsContainsCursor extends false
? ApiMethodResponse_WithoutPagination<RawData, PrettifiedData>
: ApiMethodResponse_WithPagination<RawData, PrettifiedData, ArgsContainsCursor, IsRoot>
? ApiMethodResponse_WithoutPagination<RawData, PrettifiedData> &
{ again: () => Promise<ApiMethodResponse_WithoutPagination<RawData, PrettifiedData>> }
: ApiMethodResponse_WithPagination<RawData, PrettifiedData, ArgsContainsCursor, IsRoot> &
{ again: () => Promise<ApiMethodResponse_WithoutPagination<RawData, PrettifiedData>> }

type ApiMethodResponse_WithoutPagination<RawData = any, PrettifiedData = any> = ObjectPrettify<
{
data: PrettifiedData,
response: ObjectPrettify<HttpResponse<RawData>>,
response: ObjectPrettify<HttpResponse<RawData>>
}
>

Expand Down
Loading

0 comments on commit 579f11e

Please sign in to comment.