Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination Support #1

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@


## <small>0.4.1 (2024-11-24)</small>

* chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))
* chore: revert ([fd13126](https://github.com/trycortexai/cortex-node/commit/fd13126))
* chore: update version ([fdc944d](https://github.com/trycortexai/cortex-node/commit/fdc944d))
* chore: use release --ci ([54b2b83](https://github.com/trycortexai/cortex-node/commit/54b2b83))
- chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))
- chore: revert ([fd13126](https://github.com/trycortexai/cortex-node/commit/fd13126))
- chore: update version ([fdc944d](https://github.com/trycortexai/cortex-node/commit/fdc944d))
- chore: use release --ci ([54b2b83](https://github.com/trycortexai/cortex-node/commit/54b2b83))

## <small>0.4.2 (2024-11-24)</small>

* chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))
* chore: use release --ci ([54b2b83](https://github.com/trycortexai/cortex-node/commit/54b2b83))
- chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))
- chore: use release --ci ([54b2b83](https://github.com/trycortexai/cortex-node/commit/54b2b83))

## <small>0.4.1 (2024-11-24)</small>

* chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))
- chore: fresh install packages ([038bd57](https://github.com/trycortexai/cortex-node/commit/038bd57))

## 0.4.0 (2024-11-24)

* chore: better endpoint names ([75e2323](https://github.com/trycortexai/cortex-node/commit/75e2323))
* chore: chore ([e222d7d](https://github.com/trycortexai/cortex-node/commit/e222d7d))
* chore: cleanups again ([97ab3a2](https://github.com/trycortexai/cortex-node/commit/97ab3a2))
* chore: cleanups wip ([36d089b](https://github.com/trycortexai/cortex-node/commit/36d089b))
* chore: cleanups wip ([f99ce8c](https://github.com/trycortexai/cortex-node/commit/f99ce8c))
* chore: examples ([4490f5d](https://github.com/trycortexai/cortex-node/commit/4490f5d))
* chore: get -> retrieve ([d332821](https://github.com/trycortexai/cortex-node/commit/d332821))
* chore: release ([c878e38](https://github.com/trycortexai/cortex-node/commit/c878e38))
* chore: release setup ([735c587](https://github.com/trycortexai/cortex-node/commit/735c587))
* chore: update ([5ae9fba](https://github.com/trycortexai/cortex-node/commit/5ae9fba))
* chore: update schemas ([9b64c76](https://github.com/trycortexai/cortex-node/commit/9b64c76))
* chore: update types ([03d7723](https://github.com/trycortexai/cortex-node/commit/03d7723))
* feat: client library base ([efc3499](https://github.com/trycortexai/cortex-node/commit/efc3499))
* feat: implement API parser ([1b784cd](https://github.com/trycortexai/cortex-node/commit/1b784cd))
* feat: improve endpoints generation ([ba45197](https://github.com/trycortexai/cortex-node/commit/ba45197))
* feat: streaming support ([4cfb05f](https://github.com/trycortexai/cortex-node/commit/4cfb05f))
* first commit ([d9f6bb0](https://github.com/trycortexai/cortex-node/commit/d9f6bb0))
- chore: better endpoint names ([75e2323](https://github.com/trycortexai/cortex-node/commit/75e2323))
- chore: chore ([e222d7d](https://github.com/trycortexai/cortex-node/commit/e222d7d))
- chore: cleanups again ([97ab3a2](https://github.com/trycortexai/cortex-node/commit/97ab3a2))
- chore: cleanups wip ([36d089b](https://github.com/trycortexai/cortex-node/commit/36d089b))
- chore: cleanups wip ([f99ce8c](https://github.com/trycortexai/cortex-node/commit/f99ce8c))
- chore: examples ([4490f5d](https://github.com/trycortexai/cortex-node/commit/4490f5d))
- chore: get -> retrieve ([d332821](https://github.com/trycortexai/cortex-node/commit/d332821))
- chore: release ([c878e38](https://github.com/trycortexai/cortex-node/commit/c878e38))
- chore: release setup ([735c587](https://github.com/trycortexai/cortex-node/commit/735c587))
- chore: update ([5ae9fba](https://github.com/trycortexai/cortex-node/commit/5ae9fba))
- chore: update schemas ([9b64c76](https://github.com/trycortexai/cortex-node/commit/9b64c76))
- chore: update types ([03d7723](https://github.com/trycortexai/cortex-node/commit/03d7723))
- feat: client library base ([efc3499](https://github.com/trycortexai/cortex-node/commit/efc3499))
- feat: implement API parser ([1b784cd](https://github.com/trycortexai/cortex-node/commit/1b784cd))
- feat: improve endpoints generation ([ba45197](https://github.com/trycortexai/cortex-node/commit/ba45197))
- feat: streaming support ([4cfb05f](https://github.com/trycortexai/cortex-node/commit/4cfb05f))
- first commit ([d9f6bb0](https://github.com/trycortexai/cortex-node/commit/d9f6bb0))

## 0.3.0 (2024-11-24)

* chore: better endpoint names ([75e2323](https://github.com/trycortexai/cortex-node/commit/75e2323))
* chore: chore ([e222d7d](https://github.com/trycortexai/cortex-node/commit/e222d7d))
* chore: cleanups again ([97ab3a2](https://github.com/trycortexai/cortex-node/commit/97ab3a2))
* chore: cleanups wip ([36d089b](https://github.com/trycortexai/cortex-node/commit/36d089b))
* chore: cleanups wip ([f99ce8c](https://github.com/trycortexai/cortex-node/commit/f99ce8c))
* chore: examples ([4490f5d](https://github.com/trycortexai/cortex-node/commit/4490f5d))
* chore: get -> retrieve ([d332821](https://github.com/trycortexai/cortex-node/commit/d332821))
* chore: release ([c878e38](https://github.com/trycortexai/cortex-node/commit/c878e38))
* chore: release setup ([735c587](https://github.com/trycortexai/cortex-node/commit/735c587))
* chore: update schemas ([9b64c76](https://github.com/trycortexai/cortex-node/commit/9b64c76))
* chore: update types ([03d7723](https://github.com/trycortexai/cortex-node/commit/03d7723))
* feat: client library base ([efc3499](https://github.com/trycortexai/cortex-node/commit/efc3499))
* feat: implement API parser ([1b784cd](https://github.com/trycortexai/cortex-node/commit/1b784cd))
* feat: improve endpoints generation ([ba45197](https://github.com/trycortexai/cortex-node/commit/ba45197))
* feat: streaming support ([4cfb05f](https://github.com/trycortexai/cortex-node/commit/4cfb05f))
* first commit ([d9f6bb0](https://github.com/trycortexai/cortex-node/commit/d9f6bb0))
- chore: better endpoint names ([75e2323](https://github.com/trycortexai/cortex-node/commit/75e2323))
- chore: chore ([e222d7d](https://github.com/trycortexai/cortex-node/commit/e222d7d))
- chore: cleanups again ([97ab3a2](https://github.com/trycortexai/cortex-node/commit/97ab3a2))
- chore: cleanups wip ([36d089b](https://github.com/trycortexai/cortex-node/commit/36d089b))
- chore: cleanups wip ([f99ce8c](https://github.com/trycortexai/cortex-node/commit/f99ce8c))
- chore: examples ([4490f5d](https://github.com/trycortexai/cortex-node/commit/4490f5d))
- chore: get -> retrieve ([d332821](https://github.com/trycortexai/cortex-node/commit/d332821))
- chore: release ([c878e38](https://github.com/trycortexai/cortex-node/commit/c878e38))
- chore: release setup ([735c587](https://github.com/trycortexai/cortex-node/commit/735c587))
- chore: update schemas ([9b64c76](https://github.com/trycortexai/cortex-node/commit/9b64c76))
- chore: update types ([03d7723](https://github.com/trycortexai/cortex-node/commit/03d7723))
- feat: client library base ([efc3499](https://github.com/trycortexai/cortex-node/commit/efc3499))
- feat: implement API parser ([1b784cd](https://github.com/trycortexai/cortex-node/commit/1b784cd))
- feat: improve endpoints generation ([ba45197](https://github.com/trycortexai/cortex-node/commit/ba45197))
- feat: streaming support ([4cfb05f](https://github.com/trycortexai/cortex-node/commit/4cfb05f))
- first commit ([d9f6bb0](https://github.com/trycortexai/cortex-node/commit/d9f6bb0))
8 changes: 6 additions & 2 deletions openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ const generate = async () => {

//

const typesPath = path.resolve('./src/types/openapi.ts');
fs.writeFileSync(typesPath, `${openapiTypes}\n\n${apiMethods}`);
const typesPath = path.resolve('./src/generated/openapi.ts');
fs.writeFileSync(
typesPath,
`// This file is auto-generated. Do not edit.\n\n
${openapiTypes}\n\n${apiMethods}`,
);

console.log(
'\x1b[32m%s\x1b[0m',
Expand Down
45 changes: 21 additions & 24 deletions openapi.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,25 @@ const buildAPIMethodObject = (
throw new Error('Parameters reference not supported');
}

const methodName = isResponseArray(responses)
const isPaginationSupported = (parameters as ParameterObject[])?.some(
e => e.name === 'page' && e.in === 'query',
);

const methodName = isPaginationSupported
? 'list'
: (METHOD_TO_NAME[method] ?? method);

const methodParameters = (parameters as ParameterObject[])
.filter(e => e.in === 'path')
?.filter(e => e.in === 'path')
.filter(e => e.name !== 'app_id')
.map(e => [e.name, 'string']);

const hasAnyQueryParameters = (parameters as ParameterObject[]).some(
e => e.in === 'query',
);

const responseType = getResponseType(responses);

const apiMethod: MethodOptions = {
name: methodName,
method,
Expand All @@ -203,7 +209,16 @@ const buildAPIMethodObject = (
? `paths['${endpoint}']['${method}']['parameters']['query']`
: undefined,
parameters: Object.fromEntries(methodParameters),
returns: getResponseType(responses),
returns: isPaginationSupported
? `{
data: ${responseType};
pagination: {
page: number;
take: number;
count: number;
};
}`
: responseType,
};

return apiMethod;
Expand Down Expand Up @@ -295,25 +310,6 @@ const getBodyType = (requestBody: RequestBodyObject | ReferenceObject) => {
throw new Error(`Unsupported body type: ${JSON.stringify(requestBody)}`);
};

const isResponseArray = (response: ResponsesObject | undefined) => {
const okResponse = response?.['200'];

if (!okResponse || !('content' in okResponse)) {
return false;
}

const content = okResponse.content?.['application/json'];

return (
content &&
'schema' in content &&
!!content.schema &&
'type' in content.schema &&
content.schema.type === 'array' &&
!!content.schema.items
);
};

const getResponseType = (responses: ResponsesObject | undefined) => {
const okResponse = responses?.['200'];
if (!okResponse || !('content' in okResponse)) {
Expand Down Expand Up @@ -375,6 +371,7 @@ const convertToCamelCase = (str: string) => {
};

const createMethod = ({
name,
method,
endpoint,
parameters,
Expand Down Expand Up @@ -416,7 +413,7 @@ const createMethod = ({
const parsedMethod = `(${allParameters
.map(([key, value]) => `${convertToCamelCase(key)}: ${value}`)
.join(', ')}): Promise<${returns}> => {
return callAPI({
return callAPI(${encodeParam(name)}, {
method: ${encodeParam(method)},
endpoint: ${encodeParam(endpoint)},
${paramsString}${body ? `body,` : ''}options,
Expand Down Expand Up @@ -463,7 +460,7 @@ export const methodsToString = (
}
};
export type APIMethods = ReturnType<typeof createAPI>;
export const createAPI = (callAPI: (request: APIMethodRequest) => unknown) => (${template});`;
export const createAPI = (callAPI: (name: string, request: APIMethodRequest) => unknown) => (${template});`;

return finalTemplate;
};
Loading
Loading