Skip to content

Commit

Permalink
Add svelte example, maybe batch (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
msutkowski authored Nov 6, 2020
1 parent 0b81434 commit ef742ef
Show file tree
Hide file tree
Showing 23 changed files with 1,728 additions and 70 deletions.
46 changes: 14 additions & 32 deletions examples/posts-and-counter/src/app/services/counter.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,41 @@
import { createApi } from "@rtk-incubator/simple-query/dist";
import { createApi, fetchBaseQuery } from '@rtk-incubator/simple-query/dist';

interface CountResponse {
count: number;
}

interface QueryArg {
queryString: string;
method?: string;
body?: string;
headers?: any;
}

export const counterApi = createApi({
reducerPath: "counterApi",
baseQuery({
queryString = "",
method = "GET",
body,
headers = {
"Content-Type": "application/json"
}
}: QueryArg) {
return fetch(`/${queryString}`, { method, body, headers }).then((res) =>
res.json()
);
},
entityTypes: ["Counter"], // used for invalidation
reducerPath: 'counterApi',
baseQuery: fetchBaseQuery(),
entityTypes: ['Counter'],
endpoints: (build) => ({
getCount: build.query<CountResponse, void>({
query() {
return {
queryString: `count`
queryString: `count`,
};
},
provides: [{ type: "Counter" }] // this provides entities of the type X - if you use a mutation that impacts this entity it will refetch
provides: [{ type: 'Counter' }],
}),
incrementCount: build.mutation<CountResponse, number>({
query(amount) {
return {
queryString: `increment`,
method: "PUT",
body: JSON.stringify({ amount })
method: 'PUT',
body: JSON.stringify({ amount }),
};
},
invalidates: [{ type: "Counter" }]
invalidates: [{ type: 'Counter' }],
}),
decrementCount: build.mutation<CountResponse, number>({
query(amount) {
return {
queryString: `decrement`,
method: "PUT",
body: JSON.stringify({ amount })
method: 'PUT',
body: JSON.stringify({ amount }),
};
},
invalidates: [{ type: "Counter" }]
})
})
invalidates: [{ type: 'Counter' }],
}),
}),
});
54 changes: 18 additions & 36 deletions examples/posts-and-counter/src/app/services/posts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApi } from "@rtk-incubator/simple-query/dist";
import { createApi, fetchBaseQuery } from '@rtk-incubator/simple-query/dist';

export interface Post {
id: number;
Expand All @@ -7,74 +7,56 @@ export interface Post {

type PostsResponse = Post[];

interface QueryArg {
queryString: string;
method?: string;
body?: string;
headers?: any;
}

export const postApi = createApi({
reducerPath: "postsApi",
baseQuery({
queryString,
method = "GET",
body,
headers = {
"Content-Type": "application/json"
}
}: QueryArg) {
return fetch(`/${queryString}`, { method, body, headers }).then((res) =>
res.json()
);
},
entityTypes: ["Posts"], // used for invalidation
reducerPath: 'postsApi',
baseQuery: fetchBaseQuery(),
entityTypes: ['Posts'],
endpoints: (build) => ({
getPosts: build.query<PostsResponse, void>({
query() {
return {
queryString: `posts`
queryString: `posts`,
};
},
provides: (result) => result.map(({ id }) => ({ type: "Posts", id })) // this provides entities of the type X - if you use a mutation that impacts this entity it will refetch
provides: (result) => result.map(({ id }) => ({ type: 'Posts', id })),
}),
addPost: build.mutation<Post, Partial<Post>>({
query(data) {
return {
queryString: `posts`,
method: "POST",
body: JSON.stringify(data)
method: 'POST',
body: JSON.stringify(data),
};
},
invalidates: [{ type: "Posts" }]
invalidates: [{ type: 'Posts' }],
}),
getPost: build.query<Post, number>({
query(id) {
return {
queryString: `posts/${id}`
queryString: `posts/${id}`,
};
},
provides: (result, id) => [{ type: "Posts", id }] // this provides entities of the type X - if you use a mutation that impacts this entity it will refetch
provides: (result, id) => [{ type: 'Posts', id }],
}),
updatePost: build.mutation<Post, Partial<Post>>({
query(data) {
const { id, ...post } = data;
return {
queryString: `posts/${id}`,
method: "PUT",
body: JSON.stringify(post)
method: 'PUT',
body: JSON.stringify(post),
};
},
invalidates: (result, { id }) => [{ type: "Posts", id }]
invalidates: (result, { id }) => [{ type: 'Posts', id }],
}),
deletePost: build.mutation<{ success: boolean; id: number }, number>({
query(id) {
return {
queryString: `posts/${id}`,
method: "DELETE"
method: 'DELETE',
};
},
invalidates: (_, id) => [{ type: "Posts", id }] // Will cause a refetch of `getPosts`
})
})
invalidates: (_, id) => [{ type: 'Posts', id }],
}),
}),
});
4 changes: 4 additions & 0 deletions examples/svelte-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules/
/public/build/

.DS_Store
7 changes: 7 additions & 0 deletions examples/svelte-example/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 4,
"printWidth": 120,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}
3 changes: 3 additions & 0 deletions examples/svelte-example/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}
6 changes: 6 additions & 0 deletions examples/svelte-example/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[svelte]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "svelte.svelte-vscode"
}
}
90 changes: 90 additions & 0 deletions examples/svelte-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Svelte app with msw

Just a basic test hooking up MSW for svelte trial purposes.

## Get started

Install the dependencies...

```bash
cd svelte-app
npm install
```

...then start [Rollup](https://rollupjs.org):

```bash
npm run dev
```

Navigate to [localhost:5000](http://localhost:5000). You should see your app running. Edit a component file in `src`, save it, and reload the page to see your changes.

By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the `sirv` commands in package.json to include the option `--host 0.0.0.0`.

If you're using [Visual Studio Code](https://code.visualstudio.com/) we recommend installing the official extension [Svelte for VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). If you are using other editors you may need to install a plugin in order to get syntax highlighting and intellisense.

## Building and running in production mode

To create an optimised version of the app:

```bash
npm run build
```

You can run the newly built app with `npm run start`. This uses [sirv](https://github.com/lukeed/sirv), which is included in your package.json's `dependencies` so that the app will work when you deploy to platforms like [Heroku](https://heroku.com).

## Single-page app mode

By default, sirv will only respond to requests that match files in `public`. This is to maximise compatibility with static fileservers, allowing you to deploy your app anywhere.

If you're building a single-page app (SPA) with multiple routes, sirv needs to be able to respond to requests for _any_ path. You can make it so by editing the `"start"` command in package.json:

```js
"start": "sirv public --single"
```

## Using TypeScript

This template comes with a script to set up a TypeScript development environment, you can run it immediately after cloning the template with:

```bash
node scripts/setupTypeScript.js
```

Or remove the script via:

```bash
rm scripts/setupTypeScript.js
```

## Deploying to the web

### With [Vercel](https://vercel.com)

Install `vercel` if you haven't already:

```bash
npm install -g vercel
```

Then, from within your project folder:

```bash
cd public
vercel deploy --name my-project
```

### With [surge](https://surge.sh/)

Install `surge` if you haven't already:

```bash
npm install -g surge
```

Then, from within your project folder:

```bash
npm run build
surge public my-project.surge.sh
```
32 changes: 32 additions & 0 deletions examples/svelte-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "svelte-app-rtk-simplequery-demo",
"version": "1.0.0",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public",
"validate": "svelte-check"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@rtk-incubator/simple-query": "https://pkg.csb.dev/rtk-incubator/simple-query/commit/fcea624c/@rtk-incubator/simple-query",
"@tsconfig/svelte": "^1.0.0",
"msw": "^0.21.3",
"rollup": "^2.3.4",
"rollup-plugin-inject-process-env": "^1.3.1",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^6.0.0",
"rollup-plugin-terser": "^7.0.0",
"svelte": "^3.0.0",
"svelte-check": "^1.0.0",
"svelte-preprocess": "^4.0.0",
"tslib": "^2.0.0",
"typescript": "^3.9.3"
},
"dependencies": {
"@reduxjs/toolkit": "^1.4.0",
"sirv-cli": "^1.0.0"
}
}
Binary file added examples/svelte-example/public/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions examples/svelte-example/public/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
html, body {
position: relative;
width: 100%;
height: 100%;
}

body {
color: #333;
margin: 0;
padding: 8px;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

a {
color: rgb(0,100,200);
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

a:visited {
color: rgb(0,80,160);
}

label {
display: block;
}

input, button, select, textarea {
font-family: inherit;
font-size: inherit;
-webkit-padding: 0.4em 0;
padding: 0.4em;
margin: 0 0 0.5em 0;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 2px;
}

input:disabled {
color: #ccc;
}

button {
color: #333;
background-color: #f4f4f4;
outline: none;
}

button:disabled {
color: #999;
}

button:not(:disabled):active {
background-color: #ddd;
}

button:focus {
border-color: #666;
}
Loading

0 comments on commit ef742ef

Please sign in to comment.