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

Feat models #34

Merged
merged 4 commits into from
Sep 21, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"type": "module",
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.1.0",
"@apidevtools/swagger-parser": "^10.1.0",
"@appwrite.io/pink": "0.0.7-sl10.0",
"@appwrite.io/repo": "github:appwrite/appwrite",
Expand Down
33 changes: 32 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/lib/layouts/Docs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
export let variant: DocsLayoutVariant = 'default';

const variantClasses: Record<DocsLayoutVariant, string> = {
default: 'aw-grid-side-nav aw-container',
default: 'aw-grid-side-nav',
expanded: 'aw-grid-huge-navs',
'two-side-navs': 'aw-grid-two-side-navs'
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Args = {
export const getCodeHtml = (args: Args) => {
const { content, language, withLineNumbers } = args;
const res = hljs.highlight(content, { language: language ?? 'sh' }).value;
const lines = res.split(/\n/g).slice(0, -1);
const lines = res.split(/\n/g);

while (lines.length > 0 && lines[lines.length - 1] === '') {
lines.pop();
Expand Down
85 changes: 61 additions & 24 deletions src/lib/utils/specs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import SwaggerParser from '@apidevtools/swagger-parser';
import { OpenAPIV3 } from 'openapi-types';
import { Platform, type Service } from './references';

Expand All @@ -17,13 +16,15 @@ type SDKMethod = {
responses: Array<{
code: number;
contentType?: string;
model?: {
id: string;
name: string;
};
models?: SDKMethodModel[];
}>;
};

type SDKMethodModel = {
id: string;
name: string;
};

type AppwriteOperationObject = OpenAPIV3.OperationObject & {
'x-appwrite': {
method: string;
Expand All @@ -41,7 +42,7 @@ type AppwriteOperationObject = OpenAPIV3.OperationObject & {
};
};

type AppwriteSchemaObject = OpenAPIV3.SchemaObject & {
export type AppwriteSchemaObject = OpenAPIV3.SchemaObject & {
'x-example': string;
};

Expand Down Expand Up @@ -141,7 +142,9 @@ export function getSchema(id: string, api: OpenAPIV3.Document): OpenAPIV3.Schema
throw new Error("Schema doesn't exist");
}

const specs = import.meta.glob('$appwrite/app/config/specs/open-api3*-(client|server).json');
const specs = import.meta.glob('$appwrite/app/config/specs/open-api3*-(client|server).json', {
as: 'raw'
});
async function getSpec(version: string, platform: string) {
const isServer = platform.startsWith('server-');
const target = `/node_modules/@appwrite.io/repo/app/config/specs/open-api3-${version}-${
Expand All @@ -151,10 +154,8 @@ async function getSpec(version: string, platform: string) {
}

export async function getApi(version: string, platform: string): Promise<OpenAPIV3.Document> {
const spec = await getSpec(version, platform);
const parser = new SwaggerParser();
const api = (await parser.bundle(spec as unknown as OpenAPIV3.Document)) as OpenAPIV3.Document;

const raw = await getSpec(version, platform);
const api = JSON.parse(raw);
return api;
}

Expand Down Expand Up @@ -198,28 +199,41 @@ export async function getService(
const responses: SDKMethod['responses'] = Object.entries(operation.responses ?? {}).map(
(tuple) => {
const [code, response] = tuple as [string, OpenAPIV3.ResponseObject];
const id = (
response?.content?.['application/json']?.schema as OpenAPIV3.ReferenceObject
)?.$ref
?.split('/')
.pop();
const schema = id ? getSchema(id, api) : undefined;
const models: SDKMethodModel[] = [];
const schemas = response?.content?.['application/json']?.schema as OpenAPIV3.SchemaObject;
if (code !== '204') {
if (schemas?.oneOf) {
schemas.oneOf.forEach((ref) => {
const schema = resolveReference(ref as OpenAPIV3.ReferenceObject, api);
models.push({
id: getIdFromReference(ref as OpenAPIV3.ReferenceObject),
name: schema.description ?? ''
});
});
} else {
if (schemas) {
const id = getIdFromReference(schemas as OpenAPIV3.ReferenceObject);
const schema = resolveReference(schemas as OpenAPIV3.ReferenceObject, api);
models.push({
id,
name: schema?.description ?? ''
});
}
}
}

return {
code: Number(code),
contentType: response?.content ? Object.keys(response.content)[0] : undefined,
model: id
? {
id,
name: schema?.description ?? ''
}
: undefined
models
};
}
);

const path = isAndroid
? `/node_modules/@appwrite.io/repo/docs/examples/${version}/client-android/${isAndroidJava ? 'java' : 'kotlin'}/${operation['x-appwrite'].demo}`
? `/node_modules/@appwrite.io/repo/docs/examples/${version}/client-android/${
isAndroidJava ? 'java' : 'kotlin'
}/${operation['x-appwrite'].demo}`
: `/node_modules/@appwrite.io/repo/docs/examples/${version}/${platform}/examples/${operation['x-appwrite'].demo}`;
if (!(path in examples)) {
continue;
Expand All @@ -236,3 +250,26 @@ export async function getService(

return data;
}

export function getIdFromReference(reference: OpenAPIV3.ReferenceObject) {
const id = reference?.$ref?.split('/')?.pop();
if (!id) {
throw new Error('Invalid reference');
}
return id;
}

export function resolveReference(
reference: OpenAPIV3.ReferenceObject,
api: OpenAPIV3.Document
): AppwriteSchemaObject {
const id = reference.$ref.split('/').pop();
if (!id) {
throw new Error('Invalid reference');
}
const schema = api.components?.schemas?.[id] as AppwriteSchemaObject;
if (schema) {
return schema;
}
throw new Error("Schema doesn't exist");
}
4 changes: 2 additions & 2 deletions src/markdoc/layouts/Article.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

export let title: string;
export let description: string;
export let difficulty: string;
export let readtime: string;
export let difficulty: string = '';
export let readtime: string = '';

setContext<LayoutContext>('headings', writable({}));

Expand Down
2 changes: 1 addition & 1 deletion src/routes/docs/advanced/platform/webhooks/+page.markdoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Placeholder SEO.

Certainly! Below is the content converted to Markdown format:

```markdown
```md
```php
<?php

Expand Down
6 changes: 5 additions & 1 deletion src/routes/docs/references/+page.svelte
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
placeholder
<script>
import Layout from './Layout.svelte';
</script>

<Layout>placeholder</Layout>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import Sidebar, { type NavParent, type NavTree } from '$lib/layouts/Sidebar.svelte';
import { preferredPlatform, preferredVersion } from '$lib/utils/references';

$: expandable = $page.url.pathname.startsWith('/docs/references/');
export let expandable = false;

$: prefix = `/docs/references/${$preferredVersion ?? $page.params?.version ?? 'cloud'}/${
$preferredPlatform ?? $page.params?.platform ?? 'client-web'
}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
import Layout from '../../../Layout.svelte';
</script>

<Layout expandable>
<slot />
</Layout>
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,30 @@
<div class="aw-card is-transparent u-padding-16 u-margin-block-start-16">
<ul>
{#each method.responses as response}
<li>
<article>
<header class="u-flex u-cross-baseline u-gap-8">
<h3 class="aw-eyebrow aw-u-color-text-primary">
{response.code}
</h3>
<span class="aw-caption-400">{response.model?.name}</span>
</header>
<p class="aw-sub-body-400 u-margin-block-start-16">
<a href="#">Payload <span class="icon-cheveron-right" /></a>
</p>
</article>
</li>
{#if response.models}
<li>
<article>
<header class="u-flex u-cross-baseline u-gap-8">
<h3 class="aw-eyebrow aw-u-color-text-primary">
{response.code}
</h3>
<span class="aw-caption-400">application/json</span>
</header>
<ul class="aw-sub-body-400 u-margin-block-start-16">
{#each response.models as model}
<li>
<a
class="aw-link"
href={`/docs/references/${$page.params.version}/models/${model.id}`}
>
{model.name}
</a>
</li>
{/each}
</ul>
</article>
</li>
{/if}
{/each}
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Layout from '../../../Layout.svelte';
</script>

<Layout>
<slot />
</Layout>
Loading