Giter Site home page Giter Site logo

better-rest's People

Contributors

bnomei avatar dependabot[bot] avatar kms-ploi avatar lucasdinonolte avatar moritzebeling avatar robinscholz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

better-rest's Issues

[Question] Using Betterrest Class method to JSONify a structure field

Hi there,
Sorry for bothering you again and again.
I have the case where I need to write a custom endpoint to include more information about related pages so I can't use your default rest endpoint for my page.

Certain things should be returned like they do in your code.
Specifically I have a structure field which should be returned as an Array of entries with all the fields as key.

Example:
I have a structure field in my blueprint:

         paragraphs:
            label: Paragraphs
            type: structure
            columns:
              title:
                width: 2/8
              text:
                width: 5/8
              inverted:
                width: 1/8
            fields:
              title:
                type: text
              text:
                type: textarea
              inverted:
                type: toggle

And I want it in this JSON form:

"paragraphs": [
        {
          "title": "My title",
          "text": "My text...",
          "inverted": "false"
        },
        {
          "title": "Another title",
          "text": "More text:",
          "inverted": "true"
        },

No I could do something like:

$paragraphs = $home->paragraphs()->toStructure();
$json = [];

foreach ($structures as $paragraph) {
            $json[] = [
              'title' => $paragraph->title()->toString(),
              'text' => $paragraph->text()->toString(),
              'inverted' => $paragraph->inverted()->toString(),
            ];
          }

But I thought of using some of your static methods that should be able to do the same, right?

@include_once __DIR__ . '/vendor/autoload.php';

use Robinscholz\Betterrest;

//... code for setting up a custom endpoint...

$rest = new Robinscholz\Betterrest([
  'srcset' => false,
  'kirbytags' => false,
  'smartypants' => true,
  'language' => 'de',
  'query' => [
    'select' => 'files',
  ],
]);


$content = $rest->contentFromRequest(
    new \Kirby\Http\Request([
        url' => 'pages/home' // page I want to get the content from
    ])
);

$modified = $rest->modifyContent($content);

// ... code for returning that value

Or even further:

// init new rest class instance... 
$paragraphs = $home->paragraphs();
$modified = $rest->modifyContent($paragraphs);

Sorry I am not super used to php, but as far as I understand your code:

public function modifyContent(array $array = null): ?array

You use things recursively if it is an array or not and then return the data...
If you could provide some lines of code how I could use your class methods in my own endpoint to get some nice JSON that would be awesome.

I know this is quite a bit to ask, but as I dont know anybody fluent in PHP I don't really know where else to ask...

Thank you very much in advance.

Cheers

Improvement: Ship alt or caption with srcset (or whatever fields are associated with image)

Hi. And it's me again... ๐Ÿ˜…

I just setup an image blueprint to allow adding alt tags and captions.
In a REST setup you really would like to have this kind of information directly delivered with the image.
(I would not want to make a second request to get the files of a specific page, where this information is included).

So it would be great if all fields would come with the image, along with the srcset.

If you're in lockdown (corona) and you feel like fixing this, that would be great.
Maybe I will try to make a PR, but I am not very good with kirby nor PHP... :D

Anyway, let me know what you think.

Cheers

Jones

Add markdown parsing

I think this plugin truly deserves its name for being a better rest API for Kirby. To be fair, there is not much of a use case to use the standard Kirby API, as it would require writing parsers for all the platforms that request content from the CMS (e.g. JavaScript, iOS, Android,...).

As it's obvious that Kirby is most likely used for the web and that when the API is used, it's consumed by the web platform and thus it makes sense to parse the content already to HTML, I believe that this plugin would fulfil the job even more by having the option to configure it to also parse the markdown parts of the content to HTML as well, or by doing that out of the box. As I don't really see the use case for having KirbyText parsed, but the markdown not.

While I saw in another issue that the argument against this is that this can easily be done on client-side (I agree with that), It's rather difficult for me to think of people consuming the API with KirbyText converted to HTML but without the markdown being converted. I kind of think it's either all (Better Rest plugin) or nothing (official API).

Would this be a big effort to add markdown to HTML conversion as a feature of this plugin?

Sadly, I'm not really into PHP programming and will not be able to contribute that part myself.

ps. I would even argue that the whole feature set of this plugin should be in the Kirby core, but here we are. You created the one plugin that makes Kirby relevant for decoupled and headless front-end development.

srcset generation breaks with Kirby 3.5.0's layout field

Using Kirby 3.5.1 and the latest version of better-rest (installed as a git submodule) my API calls result in a 500 error when retrieving pages with a layout field in them. The error message is "Call to a member function srcset() on null",.

As soon as I disable srcset using ?br-srcset=false in the query string I get a 200 response again.

I might find time to look into this bug myself next week, just wanted to document it.

Date format is corrupted by non-breaking space

Unfortunately the rest plugin does add a non-breaking space to a date field's return value:
image

This   is a non breaking space and it is not present if I access the endpoint without /rest
image

Just to tell you. I guess this could be classified as bug.
cheers

Kirby crashes on latest version when plugin is installed as git submodule

After Updating better-rest to its latest version (1.2.8) Kirby (3.5.3.1) throws an ErrorException on startup:

Whoops\Exception\ErrorException thrown with message "include(.../site/plugins/better-rest/vendor/composer/../../tests/kirby/src/Cms/Plugin.php): failed to open stream: No such file or directory"  Stacktrace:
#11 Whoops\Exception\ErrorException in .../kirby/vendor/composer/ClassLoader.php:444
#10 include in .../kirby/vendor/composer/ClassLoader.php:444
#9 Composer\Autoload\includeFile in .../kirby/vendor/composer/ClassLoader.php:322
#8 Composer\Autoload\ClassLoader:loadClass in [internal]:0
#7 spl_autoload_call in .../kirby/src/Cms/AppPlugins.php:748
#6 Kirby\Cms\App:plugin in .../site/plugins/better-rest/index.php:26
#5 include_once in .../kirby/src/Toolkit/F.php:418
#4 Kirby\Toolkit\F:loadOnce in .../kirby/src/Cms/AppPlugins.php:809
#3 Kirby\Cms\App:pluginsLoader in .../kirby/src/Cms/AppPlugins.php:780
#2 Kirby\Cms\App:plugins in .../kirby/src/Cms/AppPlugins.php:601
#1 Kirby\Cms\App:extensionsFromPlugins in .../kirby/src/Cms/App.php:124 #0 Kirby\Cms\App:__construct in .../index.php:5

better-rest-error

How to use Multilang? How to send an X-Language header?

Sorry for another question:
https://github.com/robinscholz/better-rest#multilang

Here I read that I can request different languages, which is great.
Unfortunately I could not get it to work.

I tried:

return [
  'debug' => true,
  'languages' => true, // enable multilang
  'smartypants' => true, // https://getkirby.com/docs/guide/content/text-formatting#smartypants
  'api' => [
    'slug' => 'api',
    'basicAuth' => true,
    'allowInsecure' => true,
    'robinscholz.better-rest.smartypants' => true,
    'robinscholz.better-rest.srcset' => [333, 777, 888, 1111],
    'robinscholz.better-rest.language' => 'en'

Which did not have any effect. I would always just get the default language.
Just to be sure:

I would request a page just normally like:

/rest/pages/home and would get the english page, if that worked?


I then tried to use an X-Language header.

Of which I've never heard.
I googled hard, but I could not really find any information for that...

I tried to send

header('X-Language: en');
// or 
header('Accept-Language: en'); // because Accept-Language seems to be a header that is sometimes used, whereas X-Language does not really have any appearance within google...
// or
// both Language headers at the same time
header('Accept-Language: en');
header('Content-Language: en');

But nothing changes my result. Always German.

Sorry for those nooby PHP questions, but PHP is not the language I use that often.

Thank you for your help.
Cheers

[Question] Using image transforms (resize) in Kirby Blocks

Hi there

I recently started using kirby blocks for content editing. When getting the blocks in the REST API I get a JSON stringified Array of blocks.
The image in there is just telling me the name (so no absolute path) and I can't use image transforms like resize or crop.
Do you have any idea how to tackle that problem?

Thanks for your help.
Cheers

Documentation a bit unclear โ€“ how do I see this plugin taking effect?

Hi there

I am sorry to bother you with this, but it seems that I have some issues with this plugin...
Maybe it is just an issue with unclear documentation ๐Ÿคทโ€โ™‚ .

Anyway, as far as I understood, after installing this plugin (moving it to site/plugins/) I should see this having an effect right?

So by default it should convert kirbytags into HTML?
When running a normal REST API command with my application (in this case Insomnia) I still get bold parts in my richtext marked as **bold**.

What could be the reason?

Then I see that one can set a few settings within the config/config.php
I don't understand how I should prefix the settings

All settings need to be prefixed with robinscholz.better-rest.!

Is it not enough to have config.php like:

return [
    'debug' => true,
    'smartypants' => true,
    'api' => [
        'slug' => 'api',
        'basicAuth' => true,
	'allowInsecure' => true,
    ],
    'srcset' => [333, 777, 888, 1111]
];

If I run the request again I don't see a source-set with these sizes.
instead I see the srcset that was added by default (before installing this plugin)

"srcset": 
"http:\/\/cms.car.test\/media\/pages\/home\/2744598729-1583104264\/img-4166rrr-352x.jpg 352w, 
http:\/\/cms.car.test\/media\/pages\/home\/2744598729-1583104264\/img-4166rrr-864x.jpg 864w, 
http:\/\/cms.car.test\/media\/pages\/home\/2744598729-1583104264\/img-4166rrr-1408x.jpg 1408w"

I do not know where these sizes com from.
Actually this is the first and only reason why I want to have this plugin,because I don't know how to get uploaded images in different sizes...

Also with using params like api/pages/home?br-srcset=375,1200. I don't see them having any effect...

Maybe you have some input on that.

Thank you in advance for your help.
I think some more examples would be helpful.

Cheers

Kirby Image Blocks crashes Betterrest

Hey unfortunately me again.
I just realized that for pages where I use Kirby Blocks with an image block the betterrest seems to crash. No data is returned whatsoever... :(

image

_Using an image block leads to no data returned..._
{
  "status": "error",
  "exception": "Error",
  "code": 0,
  "message": "Call to a member function srcset() on null",
  "details": null,
  "file": "Betterrest.php",
  "line": 180
}

I would like to fix this issue but don't really know where to start.
If you could give me some pointers on that that would be great...

[Question] Do you support pagination?

Hello,

I am thinking about using Kirby CMS as an easy flat-file headless CMS solution (NextJS as SSR ReactJS Frontend). Since I want to use it headless, I need a REST API to retrieve the data - which in my understanding, your plugin provides without any hassle to setup everything (like described here if you do configure it "manually").

The only feature that the "manual way" also provides is pagination, so that e.g. I can get all available blog posts paginated as a response. Is there such a feature also available with this plugin? E.g. what's a response if I make a request to rest/pages/ to get all blog pages? I would like to get all pages as a paginated response if possible.

If this is not possible, what's the response if I setup it the "manual" way as described above? In the docs I only see you can directly fetch the site, but how to get a list of all sites as JSON response paginated? That's essentially what I am looking for.

Thanks in advance,
-eder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.