Skip to content

Datx next example #1163

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

Open
wants to merge 166 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
166 commits
Select commit Hold shift + click to select a range
8b299f5
WIP on react package
Nov 22, 2021
ce87962
WIP on mutations
Nov 23, 2021
677bd5e
Implement useQuery hook
Nov 25, 2021
01c9221
Add eslint
Nov 25, 2021
4fae1af
Add createQuery and createMutation and fix typings
Nov 29, 2021
55ea9c3
Prepare readme
Nov 29, 2021
ad5790d
Use IResponseData type
Nov 29, 2021
6e1dd9f
WIP on reponse data interface
Nov 30, 2021
ec7fa20
Fix response data type
Dec 2, 2021
fb629cd
Update SWR
Dec 2, 2021
f581c44
Update useMutation types
Dec 2, 2021
5dd741f
WIP on SSR hydrate feature
Dec 2, 2021
85d631d
WIP on documentation for react lib
Dec 3, 2021
0233042
Finish SSR hydration
Dec 3, 2021
3a6b22e
Fix Networkutils fetch for Next.sj projects where fetch is polyfilled…
Dec 7, 2021
ab785e7
Finish SSR example
Dec 7, 2021
4b38b86
Add Layout
Dec 7, 2021
41611d8
WIP on tests setup
Dec 7, 2021
67e2ff6
Finish useQuery tests
Dec 9, 2021
d300edc
Add use mutation tests
Dec 10, 2021
41d7386
fetchQuery tests
Dec 10, 2021
f7de7cb
Cleanup
Dec 13, 2021
fc26015
Implement simgle page example
Dec 13, 2021
3f2e340
Implement useResourceList
Dec 14, 2021
3ef2ba0
Merge branch 'master' into react
Dec 24, 2021
bee51c7
WIP on better swr integration
Dec 24, 2021
06e4594
Finish client side useQuery
Jan 4, 2022
d20e739
update readme
Jan 18, 2022
812a662
Add comments
Feb 21, 2022
5dc31ce
Update readme
Feb 21, 2022
fd2f5c4
373, 375 - SSR Support and Refactoring (#691)
danijelbuhin Apr 8, 2022
77861db
Merge branch 'master' into swr
Apr 8, 2022
c9a50e5
Update lerna config
Apr 8, 2022
5857832
v2.5.0-beta.0
Apr 8, 2022
4d2de22
Fix angular build script
Apr 8, 2022
3f428e5
Revert "v2.5.0-beta.0"
Apr 8, 2022
75f0690
remove parallel
Apr 8, 2022
c99a440
ignore .angular folder
Apr 8, 2022
bc589fd
v2.5.0-beta.0
Apr 8, 2022
67d8037
Update readme
Apr 9, 2022
507721e
Fix ssr mixin types
Apr 9, 2022
f1571a6
Remove unused deps
Apr 12, 2022
365f6cc
Fix middleware typing
Apr 12, 2022
f33a901
Fix middleware typing
Apr 12, 2022
ed39b52
Fix @datx/swr versions
Apr 12, 2022
cdba4af
Add examples to lerna packages
Apr 12, 2022
6f38c75
Fix use query test
Apr 12, 2022
9d19cbf
Update SWR peer deps versions
Apr 12, 2022
039faab
Use readonly for type
Apr 12, 2022
33fcd10
Enable composite tsconfig
Apr 12, 2022
6a31faa
WIP on typings
Apr 14, 2022
4d61263
Remove mobx from bundle
Apr 14, 2022
e5bd537
Upgrade to react 18
Apr 15, 2022
680f401
Finish typings
Apr 24, 2022
29d1c5e
Update types
Apr 25, 2022
232f005
Update readme
Apr 25, 2022
96a6831
v2.5.0-beta.1
Apr 25, 2022
5e92dfc
Fix readme typo
Apr 25, 2022
be5a089
Fix readme typos
Apr 25, 2022
fdb67e1
Fix peer deps
Apr 28, 2022
331b5d9
Add prefetch option to fetchQuery
May 3, 2022
3b933ea
Rename useSafeClient to useInitialize
May 6, 2022
357cfaa
Rename useQuery to useDatx
May 6, 2022
e7df87f
v2.5.0-beta.2
May 6, 2022
1b76e37
Add env
May 11, 2022
4f4e8db
Update gitignore
May 11, 2022
d902f27
Update SWR version
May 17, 2022
8a6c47e
Add typings to example
May 24, 2022
f2ba01f
Add defining models to swr readme (#879)
gbareza May 24, 2022
2b4628f
Update client instance type on useClient hook
Jun 21, 2022
2516adc
Expose DatxConfiguration
Jun 21, 2022
2a5f13f
v2.5.0-beta.3
Jun 21, 2022
7e4eba4
Merge branch 'master' into swr
Jul 12, 2022
f629882
Add some scripts
Jul 12, 2022
7788556
Update packages
Jul 12, 2022
c8435cd
Optimise rerenders if data stays the same
Jul 13, 2022
11ccdbb
Update testing deps
Jul 13, 2022
b3347c6
Add tests for compare
Jul 13, 2022
b8fe0c2
v2.5.0-beta.4
Jul 13, 2022
dcb68c1
Implement getResponseRawData helper
Jul 15, 2022
ae565e5
Fix mobx warnings in terminal
Jul 15, 2022
b462e61
Update compare to use getResponseRawData
Jul 15, 2022
864d8c5
Cleanup
Jul 15, 2022
8183d9a
v2.5.0-beta.5
Jul 15, 2022
160eac0
Merge branch 'master' into swr
Oct 26, 2022
281a354
Merge branch 'master' into swr
Nov 3, 2022
756e237
Update README.md
Nov 29, 2022
627b78b
Merge branch 'master' into swr
Dec 5, 2022
d8e50c8
Revert jsonapi-angular
Dec 5, 2022
b80d9d1
Revert and fix issues
Dec 6, 2022
46fcdf1
Mark jsonapi decorator as deprecated
Dec 6, 2022
9dce296
Revert jsonapi-angular
Dec 6, 2022
812bd44
Fix ts issues
Dec 6, 2022
370209b
Merge branch 'master' into swr
Dec 6, 2022
533cca4
Fix ts issues
Dec 6, 2022
c4cf343
Create separate responses
Dec 6, 2022
79b6463
Get related redcord (#1132)
Dec 6, 2022
d196ac8
Allow prefetch to be function
Dec 6, 2022
6730b89
Remove v8 from jest.config
Dec 6, 2022
9028ebe
Revert lerna stuff
Dec 6, 2022
bc5bca6
Write fetch-query tests
Dec 6, 2022
779a475
Update compare tests
Dec 6, 2022
620bfbb
Fix use-mutation tests
Dec 6, 2022
49282a5
Add id to related resource expression
Dec 7, 2022
adbbf53
Add moduleNameMapper to jest
Dec 7, 2022
e72b566
Add type support for getAll queries
Dec 7, 2022
2cd6ff0
Write test for getAll
Dec 7, 2022
397154e
Update hydrate to work with fetchAll
Dec 7, 2022
61d6766
Fix lint issues
Dec 7, 2022
c0be079
Remove packages since we are using workspaces
Dec 7, 2022
205eed7
Update tests.yml
Dec 7, 2022
dbd70fd
Update gh actions
Dec 7, 2022
e84f08d
Fix mobx install
Dec 7, 2022
27d667c
Add node_module cache
Dec 7, 2022
e52bea9
Revert yarn workspaces change
DarkoKukovec Dec 8, 2022
526a4d7
Tweak the caching key
DarkoKukovec Dec 8, 2022
d471e27
Skip nx cache in CI
Dec 9, 2022
77353a7
Use src files for datx libs in tests
Dec 13, 2022
bf52ef4
Add getRelatedResource and getRelatedResources query support
Dec 13, 2022
3a74119
Remove leftover comments
Dec 13, 2022
a3d5f8c
Fix failing build
Dec 13, 2022
2b50898
Use lerna commands instead of yarn
Dec 13, 2022
38deba1
Add no-private flag to build and test
Dec 13, 2022
053721b
Revert gh action deps
Dec 14, 2022
7a96452
Skip lerna cache
Dec 14, 2022
e678f38
Update useDatx types
Dec 14, 2022
5f8e7f6
Set no-private on publish script
Dec 14, 2022
7d69024
Add beta publish script
DarkoKukovec Dec 14, 2022
525374a
v2.5.0-beta.6
DarkoKukovec Dec 14, 2022
3d950b7
Update rollup config
Dec 14, 2022
4dcc886
Remove files from package.json
Dec 14, 2022
9530e64
v2.5.0-beta.7
Dec 14, 2022
7578849
Export Response and Fallback from the repo
Dec 14, 2022
a763ed7
v2.5.0-beta.8
Dec 14, 2022
92956ae
Fix endpoint not being used when fetching related resources
Dec 16, 2022
2a8c20a
v2.5.0-beta.9
Dec 19, 2022
b4b21dc
Swr Infinite (#1145)
Jan 31, 2023
9ac0ddb
Add useDatxInfinite usage to README.md
Jan 31, 2023
7512897
v2.5.0-beta.10
Feb 1, 2023
b3a8bc8
Merge branch 'swr'
Mar 10, 2023
4f14305
Add SWR docs
Mar 20, 2023
aef9fa4
Add `requestSingle` and `requestCollection`
Mar 20, 2023
1fd3b68
SWR to v2
Mar 20, 2023
cd59004
Update nextjs example
Mar 20, 2023
6a3f2ee
Revert tests.yml
Mar 20, 2023
503d53f
Unrevert test.yml
Mar 21, 2023
52c45b8
Add setup-node@v3
Mar 21, 2023
d7435cd
Install mobx to datx core and utils
Mar 21, 2023
a523eaf
Revert sidebars
Mar 21, 2023
d13cfe7
v2.5.0-beta.11
Mar 23, 2023
66be78c
Update yarn lock
Mar 23, 2023
9537171
Merge branch 'master' into swr
Mar 28, 2023
76783d6
rename folder to datx-swr
Mar 28, 2023
d7a8475
Update .eslintrc
Mar 28, 2023
0cb6f88
Merge branch 'master' into swr
Mar 29, 2023
2661c38
Revert workspace config
Mar 29, 2023
fb1c038
Remove examples
Mar 29, 2023
b40cb25
Revert examples basic-seutp docs
Mar 29, 2023
9d38b6b
Revert packages/datx-jsonapi/src/Response.ts
Mar 29, 2023
5e846ce
Remove js-dom env lib
Mar 29, 2023
5b934bd
Remove docs
Mar 29, 2023
db5d6a0
Merge branch 'master' into swr
Mar 29, 2023
7beb127
Merge branch 'master' into swr
Mar 29, 2023
c9d0abb
Revert "Remove examples"
Mar 29, 2023
bdfb9f2
Add script
Mar 29, 2023
a214b97
Merge branch 'master' into datx-next-example
Mar 30, 2023
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 examples/nextjs/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_JSONAPI_URL="http://localhost:3000/api/jsonapi/"
1 change: 1 addition & 0 deletions examples/nextjs/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_JSONAPI_URL="http://localhost:3000/api/jsonapi/"
4 changes: 4 additions & 0 deletions examples/nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": ["@infinumjs/eslint-config-react-ts", "plugin:@next/next/recommended"]
}
40 changes: 40 additions & 0 deletions examples/nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
.next

# typescript
*.tsbuildinfo

!/typings
29 changes: 29 additions & 0 deletions examples/nextjs/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev"
},
{
"name": "Next.js: debug client-side",
"type": "pwa-chrome",
"request": "launch",
"url": "http://localhost:3000"
},
{
"name": "Next.js: debug full stack",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev",
"console": "integratedTerminal",
"serverReadyAction": {
"pattern": "started server on .+, url: (https?://.+)",
"uriFormat": "%s",
"action": "debugWithChrome"
}
}
]
}
34 changes: 34 additions & 0 deletions examples/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Empty file added examples/nextjs/mobx.js
Empty file.
5 changes: 5 additions & 0 deletions examples/nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
18 changes: 18 additions & 0 deletions examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const withPlugins = require('next-compose-plugins');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});

/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
productionBrowserSourceMaps: true,
typescript: {
ignoreBuildErrors: true,
},
eslint: {
ignoreDuringBuilds: true,
},
};

module.exports = withPlugins([[withBundleAnalyzer], config]);
38 changes: 38 additions & 0 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "nextjs",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "NODE_OPTIONS='--inspect' next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"analyze": "ANALYZE=true yarn build",
"analyze:sourcemap": "source-map-explorer .next/static/**/*.js"
},
"dependencies": {
"@datx/core": "^2.5.0-beta.0",
"@datx/jsonapi": "^2.5.0-beta.1",
"@datx/swr": "^2.5.0-beta.3",
"@next/bundle-analyzer": "^12.2.2",
"mobx": "^6.6.0",
"next": "^13.2.4",
"next-api-router": "^1.0.4",
"next-compose-plugins": "2.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"swr": "^2.1.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@infinumjs/eslint-config-react-ts": "^2.9.0",
"@next/eslint-plugin-next": "12.1.5",
"@types/node": "^17.0.41",
"@types/react": "^18.0.12",
"@types/uuid": "^8.3.4",
"eslint": "^8.17.0",
"eslint-config-next": "12.0.4",
"source-map-explorer": "2.5.2",
"typescript": "~4.7.3"
}
}
Binary file added examples/nextjs/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/nextjs/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/nextjs/src/api/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const db = new Map();

db.set('todos', [{ id: '1', message: 'test 1' }, { id: '2', message: 'test 2' }]);
db.set('posts', [{ id: '1', title: 'Title 1', body: 'Body 1' }, { id: '2', title: 'Title 2', body: 'Body 2' }]);
55 changes: 55 additions & 0 deletions examples/nextjs/src/api/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { IModelConstructor, PureModel } from '@datx/core';
import { NextApiRequest, NextApiResponse } from 'next';
import { v4 } from 'uuid';

import { db } from './db';
import { serializeMany, serializeOne, Record, serializeErrors } from './serializer';

const findModel = (type: string) => (model: IModelConstructor<PureModel>) => model.type === type;
const findRecord = (id: string) => (record: Record) => record.id === id;

interface IHandlerSettings {
types: Array<IModelConstructor<PureModel>>;
}

export const createHandler =
({ types }: IHandlerSettings) =>
(req: NextApiRequest, res: NextApiResponse) => {
const {
query: { slug },
} = req;

const [type, id] = slug as Array<string>;

const Model = types.find(findModel(type));

if (Model) {
let records = db.get(type);

if (id) {
const record = records.find((item: Record) => item.id === id);

res.status(200).json(serializeOne(record, type));
return;
}

if (req.method === 'POST') {
const id = v4();
const {
data: { attributes },
} = JSON.parse(req.body);
const record = db
.set(type, [...records, { id, ...(attributes || {}) }])
.get(type)
.find(findRecord(id));

res.status(201).json(serializeOne(record, type));
return;
}

res.status(200).json(serializeMany(records, type));
return;
}

res.status(404).json(serializeErrors([{ status: 404, title: 'Not Found ' }]));
};
47 changes: 47 additions & 0 deletions examples/nextjs/src/api/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export interface Record {
id: string;
}

export function serializeRecord<T extends Record = Record>(data: T, type: string) {
if (!data) {
return undefined;
}

const { id, ...attributes } = data;

return {
id,
type,
attributes
}
}

export function serializeOne<T extends Record = Record>(data: T, type: string) {
return {
data: serializeRecord(data, type) || null,
}
}

export function serializeMany<T extends Record = Record>(data: Array<T>, type: string) {
return {
data: data?.map((record) => serializeRecord(record, type)) || null
}
}

interface IJsonApiError {
status: number;
title: string;
}

export function serializeError({ status, title }: IJsonApiError) {
return {
status,
title
}
}

export function serializeErrors(errors: Array<IJsonApiError>) {
return {
errors
};
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IGetManyExpression } from '@datx/swr';
import { Post } from 'src/models/Post';

export const postsQuery: IGetManyExpression<typeof Post> = {
op: 'getMany',
type: Post.type,
};
34 changes: 34 additions & 0 deletions examples/nextjs/src/components/features/posts/Posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useDatx } from '@datx/swr';
import { FC, useState } from 'react';

import { ErrorFallback } from '../../shared/errors/ErrorFallback/ErrorFallback';
import { postsQuery } from './Posts.queries';

export const Posts: FC = () => {
const [pageIndex, setPageIndex] = useState(0);

const { data, error } = useDatx(postsQuery);

if (error) {
return <ErrorFallback error={error} />;
}

if (!data) {
return <div>Loading...</div>;
}

return (
<div>
{data.data?.map((post) => (
// <NextLink href={`${base}/todos/${post.id}`} key={post.id}>
<a style={{ display: 'block' }} key={post.id}>
{post.body}
</a>
// </NextLink>
))}

<button onClick={() => setPageIndex(pageIndex - 1)}>Previous</button>
<button onClick={() => setPageIndex(pageIndex + 1)}>Next</button>
</div>
);
};
11 changes: 11 additions & 0 deletions examples/nextjs/src/components/features/todo/Todo.queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IGetOneExpression } from '@datx/swr';
import { Todo } from '../../../models/Todo';

export const getTodoQuery = (id?: string) =>
id
? ({
id,
op: 'getOne',
type: 'todos',
} as IGetOneExpression<typeof Todo>)
: null;
24 changes: 24 additions & 0 deletions examples/nextjs/src/components/features/todo/Todo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useDatx } from '@datx/swr';
import { FC } from 'react';

import { ErrorFallback } from '../../shared/errors/ErrorFallback/ErrorFallback';

import { getTodoQuery } from './Todo.queries';

export interface ITodoProps {
id?: string;
}

export const Todo: FC<ITodoProps> = ({ id }) => {
const { data, error } = useDatx(getTodoQuery(id));

if (error) {
return <ErrorFallback error={error} />;
}

if (!data) {
return <div>Loading todo...</div>;
}

return <div>{data?.data?.message}</div>;
};
12 changes: 12 additions & 0 deletions examples/nextjs/src/components/features/todos/Todos.mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getModelEndpointUrl, modelToJsonApi } from '@datx/jsonapi';
import { IClientInstance } from '@datx/swr';

import { Todo } from '../../../models/Todo';

export const createTodo = (client: IClientInstance, message: string | undefined) => {
const model = new Todo({ message });
const url = getModelEndpointUrl(model);
const data = modelToJsonApi(model);

return client.requestSingle<Todo>(url, 'POST', { data });
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IGetManyExpression } from '@datx/swr';

import { Todo } from '../../../models/Todo';

export const todosQuery: IGetManyExpression<typeof Todo> = {
op: 'getMany',
type: 'todos',
};
Loading